我只是想使用循环用字符串填充数组。我的问题是,当它进入循环输入名称时,它会立即为向量中的第一个插槽输入一个空行。为什么会这样?我该如何解决它。请不要介意我缺乏代码风格,在今年冬天开始上课之前,我是一个试图重新获得编程知识的菜鸟......
这是一些示例输出:
How many people are in your family?
4
Please enter the names of all of your family members
check name:
Please enter the names of all of your family members
Matt
check name:Matt
Please enter the names of all of your family members
Evan
check name:Evan
Please enter the names of all of your family members
Michelle
check name:Michelle
Matt
Evan
Michelle
这是我的代码:
vector<string> Names;
bool complete=0;
while(!complete)
{
int number;
cout << "How many people are in your family?" << endl;
cin >> number;
for(int i=0; i<number; i++)
{
string names;
cin.clear();
cout << "Please enter the names of all of your family members" << endl;
getline(cin,names);
Names.push_back(names);
cout << "check name:" << names << endl;
}
complete = 1;
}
for (int i=0; i< Names.size(); i++)
{
cout << Names[i] << endl;
}