C++的输入
1. cin的返回值
当cin能够读取到合法值的时候,cin返回其自身的引用,当读到不合法的值的返回0。
可以通过while语句多次读取:
#include <iostream>
using namespace std;
int main()
{
int a;
while (cin >> a)
{
cout << "right" << endl;
}
cout << "false" << endl;
return 0;
}
1
right
2
right
3
right
a
false
2. 读入一个字符
使用cin(不能读入空格和回车,遇到空格或回车就跳过)
#include <iostream>
using namespace std;
int main()
{
char a;
while (cin >> a)
{
co

本文详细介绍了C++中处理输入的方法,包括cin的返回值、读入字符、读入字符串以及cin.get()和cin.getline()的使用区别。通过示例代码展示了如何处理不同情况下的输入,特别是在处理空格和回车时的差异。
最低0.47元/天 解锁文章
9158

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



