簡體   English   中英

完全陌生的載體。 向量下標超出范圍

[英]Completely new to vectors. Vector subscript out of range

對於我的作業,我應該將一組姓名和號碼放入向量中,然后將它們用作電話簿。 值得一提的是,我正在將代碼從使用數組的原始版本轉換為每個分配。 現在,不幸的是,我的教授提供的講座只涵蓋了一些絕對的基礎知識,幾乎沒有展示任何細節,所以我只能挖掘大部分內容。 我知道錯誤可能與向量的大小有關,但我不知道我做錯了什么或如何解決它。

int main()
{
    //variables
    string eleName;
    int elePhone;
    char choice;
    vector<string> name;
    name.push_back(string(10, 10));
    name = { "Andrico", "Bonnie", "Charles",
                        "Debbie", "Enrique", "Felicia" };
    vector<int> phone;
    phone.push_back(10);
    phone = { 5551234,
                        5555678,
                        5552468,
                        5551379,
                        5559876,
                        5554321 };



    do
    {
        //call function showPhoneBook
        showPhoneBook(name, phone, MAX);

        //get user request
        cout << "\n\nWho\'s phone number do you want to see?" << endl;
        cout << "remember, type \'e\' for edit and \'x\' for exit, otherwise just type the number of the person." << endl;
        cin >> choice;

        //call function toChoice
        elePhone = toChoice(choice);
        if (elePhone != -1)
        {
            cout << "The phone number for " << name[elePhone] << " is " << phone[elePhone] << ".\n\n\n\n\n\n";
        }
        if (choice == 'e')
        {
            int choice1;
            cout << "Which entry do you want to edit?  ";
            cin >> choice1;
            editNum(name, phone, MAX, choice1);
        }
    } while (choice != 'x');

    return 0;
}

void showPhoneBook(vector<string> N, vector<int> P, int size)
{
    for (int i = 0; i < size; i++)
    {
        cout << i << "  " << N[i] << "\t" << P[i] << endl;
        cout << "Size of the phone book " << N.size() << endl;

    }
    return;
}

int toChoice(char c)
{
    int num=0;
    if (c != 'e' && c != 'x')
    {
        switch (c)
        {
        case '0':
            num = 0;
            return num;
        case '1':
            num = 1;
            return num;
        case '2':
            num = 2;
            return num;
        case '3':
            num = 3;
            return num;
        case '4':
            num = 4;
            return num;
        case '5':
            num = 5;
            return num;
        case '6':
            num = 6;
            return num;
        case '7':
            num = 7;
            return num;
        case '8':
            num = 8;
            return num;
        case '9':
            num = 9;
            return num;

        }

    }
    else
        return -1;
    return num;
}

void editNum(vector<string> N, vector<int> P, int size, int c1)
{
    string name;
    int number;
    cout << "What is the name for entry " << c1 << "?:  ";
    cin >> name;
    cout << "What is the number for entry " << c1 << "?:  ";
    cin >> number;

    N[c1] = name;
    P[c1] = number;
    return;
}
vector<int> phone;
    phone.push_back(10);
    phone = { 5551234,
                        5555678,
                        5552468,
                        5551379,
                        5559876,
                        5554321 };

phone.push_back(10) 實際上就像 phone[0] = 10;
我想你是故意的
"phone.resize(10); "這意味着'phone'有10個大小。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM