C++程序中cin.get()的作用

#include<iostream>#include<string>#include<vector>constintNUM=5;intmain(){usingstd::v... #include <iostream>
#include <string>
#include <vector>

const int NUM = 5;
int main()
{
using std::vector;
using std::string;
using std::cin;
using std::cout;
using std::endl;

vector<int> ratings(NUM);
vector<string> titles(NUM);
cout << "You will do exactly as told. You will enter\n"
<< NUM << " book titles and your ratings (0-10).\n";
int i;
for (i = 0; i < NUM; i++)
{
cout << "Enter title #" << i + 1 << ": ";
getline(cin,titles[i]);
cout << "Enter your rating (0-10): ";
cin >> ratings[i];
cin.get();
}
cout << "Thank you. You entered the following:\n"
<< "Rating\tBook\n";
for (i = 0; i < NUM; i++)
{
cout << ratings[i] << "\t" << titles[i] << endl;
}

return 0;
}

在该程序第一个for循环中cin.get();的意义在于什么?为什么输入流中会残留一个回车符?
展开
 我来答
物理公司的
2017-08-27 · TA获得超过5695个赞
知道大有可为答主
回答量:6105
采纳率:86%
帮助的人:1305万
展开全部
cin.get()是保留回车在输入流队列中的.而cin是丢弃回车的.因为你要结束输入要以回车会标志,就看你要不要保留
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式