【发布时间】:2015-04-22 05:44:30
【问题描述】:
我试图在我的 do while 循环中使用一个计数器,但如果我这样做了,它会在第一次迭代中切断我的 getline 的第一个字母
这里是 do while 循环..
do
{
cout << "Enter the name of your class: " << endl;
cin.get();
getline(cin, classnames[i]);
cout << "Enter how many units was the class: " << endl;
cin >> classunits[i];
cout << "Enter the grade you completed the class with: " << endl;
cin >> classgrades[i];
classgrades[i] = toupper(classgrades[i]);
i++;
} while (again());
例如,如果我输入 english,'e' 将在第一次迭代中被切断,但任何下一次迭代 english 都会显示正常。
只有当我添加i++时才会发生这种情况
我已经尝试过cin.ignore(),但我似乎仍然无法破解它!
【问题讨论】:
-
好吧
cin.get()读取一个字符。大概就是这样。 -
你为什么用这个
cin.get()?它只是读取一个字符并丢弃它 -
hm 当我删除它时,它会在循环时显示输入名称/输入多少单位。对不起,我还是很新哈哈
标签: c++ arrays increment getline