#include <iostream>
using namespace std;
#include <string>
int main()
{
string str;
char buf[20];
cout << "input a line" << endl;
getline(cin, str);
cout << "str=" << str << endl;
cout << "input a line <20" << endl;
cin.getline(buf, 20);
cout << "buf=" << buf << endl;
if (!cin)
{
cout << "read erro state" << endl;
}
int n;
cout << "input an integer" << endl;
cin >> n;
cout << "n=" << n << endl;
return 0;
}getline c分格和c++风格
最新推荐文章于 2025-08-07 15:04:09 发布
本文介绍了一个使用C++进行字符串处理及标准输入输出操作的示例程序。演示了如何从用户那里接收一行文本并存储到string类型变量中,同时展示了如何通过字符数组接收不超过20个字符的输入。此外,还提供了读取整数的例子,并处理了可能的输入错误。
18

被折叠的 条评论
为什么被折叠?



