对于我的任务,我应该将一组名称和数字放入一个矢量中,然后将它们用作电话簿。值得一提的是,我正在从原始版本转换代码,该版本使用每个分配的数组。现在,不幸的是,我的教授提供的讲座仅涵盖了一些绝对的基础知识,几乎没有显示任何细节,因此我不得不去研究大部分内容。我知道错误可能与向量的大小有关,但我不知道我在做什么或如何解决。
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;
}
答案 0 :(得分:0)
vector<int> phone;
phone.push_back(10);
phone = { 5551234,
5555678,
5552468,
5551379,
5559876,
5554321 };
phone.push_back(10)实际上像phone [0] = 10;
我想你打算
“ phone.resize(10);”
这意味着“电话”的大小为10。