cin.getline()较大
问题描述:
#include<iostream>
using namespace std;
int main()
{
char test[10];
char cont[10];
cin.getline(test,10);
cin.getline(cont,10);
cout<<test<<" is not "<<cont<<endl;
return 0;
}
当我输入时:
12345678901234567890
12345678901234567890
输出为:
123456789
123456789
似乎 cont
为空.有人可以解释吗?
It seems cont
is empty. Could someone explain it?
答
istream :: getline
会设置失败位,从而阻止了进一步的输入.将您的代码更改为:
istream::getline
sets the fail bit if the input is too long, and that prevents further input. Change your code to:
#include<iostream>
using namespace std;
int main()
{
char test[10];
char cont[10];
cin.getline(test,10);
cin.clear(); // add this
cin.getline(cont,10);
cout<<test<<" is not "<<cont<<endl;
return 0;
}