数据输入(input)语法: cin >> 变量名;
“>>”为右运算符
“cin” 可以输入多种数据类型(详见代码)
下面为一个实例
#include<iostream>
using namespace std;
int main15() {
//1、整型
int a = 0;
cout << "请给整型变量a赋值: " << endl;
cin >> a;
cout << "整型变量a = " << a << endl;
//2、浮点型
float f = 3.14f;
cout << "请给浮点型变量f赋值: " << endl;
cin >> f;
cout << "浮点型变量f = " << f << endl;
//3、字符型
char ch = 'a';
cout << "请给字符型变量ch赋值: " << endl;
cin >> ch;
cout << "字符型变量ch = " << ch << endl;
//4、字符串型
string str = "hello world";
cout << "请给字符串型变量str赋值: " << endl;
cin >> str;
cout << "字符串型变量str = " << str << endl;
//5、bool型
bool flag = false;
cout << "请给bool型变量flag赋值: " << endl;
cin >> flag;
cout << "bool型变量flag = " << flag << endl;
system("pause");
return 0;
}
输出结果为
在bool型变量中除“0”以外其他的数字输出结果都为“1”(即为真)