一、读取单个变量
1、读取单个数字
#include <iostream>
using namespace std;
int main() {
int num;
cout << "请输入一个数字:";
cin >> num;
cout << "数字:" << num << endl;
return 0;
}
- 输出一个数字,输出结果
请输入一个数字:1
数字:1
- 输入一个非数字,输出结果
请输入一个数字:q
数字:0
2、读取单个字符
#include <iostream>
using namespace std;
int main() {
char c;
cout << "请输入一个字符:";
cin >> c;
cout << "字符:" << c << endl;
return 0;
}
- 输入一个字符,输出结果
请输入一个字符:q
字符:q
- 输入多个字符,输出结果
请输入一个字符:qwe
字符:q
二、读取多个变量
1、具体实现 1
#include <iostream>
using namespace std;
int main() {
int a, b;
cout << "请输入两个数字(空格分隔):";
cin >> a >> b;
cout << "和为:" << a + b << endl;
return 0;
}
- 输出结果
请输入两个数字(空格分隔):123 456
和为:579
2、具体实现 2
#include <iostream>
#include <string>
using namespace std;
int main() {
int num1;
cout << "请输入第一个数字:";
cin >> num1;
cout << "第一个数字:" << num1 << endl;
int num2;
cout << "请输入第二个数字:";
cin >> num2;
cout << "第二个数字:" << num2 << endl;
return 0;
}
- 输出结果
请输入第一个数字:123
第一个数字:123
请输入第二个数字:456
第二个数字:456
三、读取字符串
1、读取不含空格的字符串
#include <iostream>
#include <string>
using namespace std;
int main() {
string str;
cout << "输入内容(不带空格):";
cin >> str;
cout << "内容:" << str << endl;
return 0;
}
- 输入一段字符串,不含空格
输入内容(不带空格):123
内容:123
- 输入一段字符串,含空格
输入内容(不带空格):123 45
内容:123
2、读取含空格的字符串
#include <iostream>
#include <string>
using namespace std;
int main() {
string str;
cout << "输入内容:";
getline(cin, str);
cout << "内容:" << str << endl;
return 0;
}
- 输入一段字符串,不含空格
输入内容:123
内容:123
- 输入一段字符串,含空格
输入内容:123 45
内容:123 45
四、错误输入
1、基本介绍
-
合法输入时,
cin.fail()
返回 false -
非法输入时,
cin.fail()
返回 true
2、演示
#include <iostream>
#include <string>
using namespace std;
int main() {
int age;
cout << "请输入你的年龄:";
if (cin >> age) {
cout << "你的年龄是:" << age << endl;
}
else {
cout << "非法的输入" << endl;
}
cout << cin.fail() << endl;
return 0;
}
- 合法输入,输出结果
请输入你的年龄:20
你的年龄是:20
0
- 非法输入,输出结果
请输入你的年龄:qwe
非法的输入
1
五、错误输入的影响
1、基本介绍
-
如果存在错误状态,cin 后续的读取都会直接跳过
-
直到清除错误状态,cin 后续的读取才会恢复正常
2、影响演示
#include <iostream>
#include <string>
using namespace std;
int main() {
int age;
cout << "请输入你的年龄:";
if (cin >> age) {
cout << "你的年龄是:" << age << endl;
}
else {
cout << "非法的输入" << endl;
}
cout << cin.fail() << endl;
int num;
cout << "请输入一个数字:";
cin >> num;
cout << "数字:" << num << endl;
return 0;
}
- 输出结果
请输入你的年龄:qwe
非法的输入
1
请输入一个数字:数字:0
3、影响演示解读
-
cin >> age
尝试读取整数 -
但用户输入的是字母,
cin.fail()
变为true
-
age
的值未被修改,可能是未初始化的随机值 -
无效数据仍留在输入缓冲区中
-
后续输入
cin >> num
被跳过
六、错误输入的处理
1、基本介绍
-
调用
cin.clear()
清除错误状态 -
调用
cin.ignore()
清空缓冲区残留数据
2、演示
#include <iostream>
#include <string>
using namespace std;
int main() {
int age;
cout << "请输入你的年龄:";
if (cin >> age) {
cout << "你的年龄是:" << age << endl;
}
else {
cout << "非法的输入" << endl;
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n'); // 丢弃缓冲区中的无效数据,直到遇到换行符 \n 或者达到最大字符数
}
cout << cin.fail() << endl;
int num;
cout << "请输入一个数字:";
cin >> num;
cout << "数字:" << num << endl;
return 0;
}
- 输出结果
请输入你的年龄:qwe
非法的输入
0
请输入一个数字:123
数字:123
七、读取字符串问题
1、问题演示
#include <iostream>
#include <string>
using namespace std;
int main() {
string str1;
cout << "输入内容 1(不带空格):";
cin >> str1;
cout << "内容 1:" << str1 << endl;
string str2;
cout << "输入内容 2(不带空格):";
cin >> str2;
cout << "内容 2:" << str2 << endl;
return 0;
}
- 输出结果
输入内容 1(不带空格):123 456
内容 1:123
输入内容 2(不带空格):内容 2:456
2、问题演示解读
-
用户输入
123 456
时 -
cin >> str1
读取123
-
剩下的数据仍留在输入缓冲区中
-
cin >> str2
读取456
3、问题处理演示
#include <iostream>
#include <string>
using namespace std;
int main() {
string str1;
cout << "输入内容 1(不带空格):";
cin >> str1;
cout << "内容 1:" << str1 << endl;
cin.ignore(numeric_limits<streamsize>::max(), '\n'); // 丢弃缓冲区中的无效数据,直到遇到换行符 \n 或者达到最大字符数
string str2;
cout << "输入内容 2(不带空格):";
cin >> str2;
cout << "内容 2:" << str2 << endl;
return 0;
}
- 输出结果
输入内容 1(不带空格):123 456
内容 1:123
输入内容 2(不带空格):123
内容 2:123
八、连续读取
#include <iostream>
#include <string>
using namespace std;
int main() {
while (true)
{
string str;
cout << "输入内容:";
getline(cin, str);
cout << "内容:" << str << endl;
}
return 0;
}
- 输出结果
输入内容:123
内容:123
输入内容:456
内容:456
...