【发布时间】:2012-09-11 07:13:18
【问题描述】:
我在计算机实验室,没有一个导师能弄清楚为什么我的getline 不能正常工作。它没有正确存储信息(仅存储 1 或 2 个字母)。有谁知道为什么会这样?
void addMovie(Inventory movie[], int &count)
{
string s;
int i;
cout << "Please enter the SKU " << endl;
cin >> i;
movie[count].sku = i;
cout << "Please enter the name of the movie you wish to add " << endl;
cin.ignore('\n');
getline(cin, s, '\n');
movie[count].title = s;
count++;
}
【问题讨论】:
-
请注意,您可能应该使用某种标准容器而不是 C 样式数组。您似乎没有在这里进行任何边界检查。
-
您也不必在
getline上输入'\n'。这是默认分隔符。