gpt4 book ai didi

c++ - 尺寸较大的 cin.getline( )

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:11:44 24 4
gpt4 key购买 nike

#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

输出是:

123456789

cont 似乎是空的。谁能解释一下?

最佳答案

如果输入太长,

istream::getline 会设置失败位,从而阻止进一步的输入。将您的代码更改为:

#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;
}

关于c++ - 尺寸较大的 cin.getline( ),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6053957/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com