当使用c++标准输入/输出的变量,函数以及不带参数的操纵器时,必须包含头文件iostream;
cin-标准输入 cout-标准输出 cerr-标准出错;
<<输出 >>输入,不需要格式化
例:输入一个int和一个float数据并输出
#include<iostream>
using namespace std;
int main(){
int id;
float av;
cout << "enter the id and the average:";
cin >> id >> av;
cout << "ID " << id << '\n'
<< "AV " << av << '\n';
return 0;
}
如 cin >> val, 若一个值成功输入val 中,则这个表达式返回真,否则为假 例:
#include<iostream>
using namespace std;
int main(){
int val;
int sum = 0;
cout << "enter next number: ";
while(cin >> val){
sum += val ;
cout << "enter next number: ";
}
cout << "sum of all values " << sum << '\n';
return 0;
}
输入和输出可以被操作符格式化;
dec-十进制
hex-十六进制
oct-八进制
endl-刷新缓冲区并换行
flush-刷新输出流
left-左对齐
right-右对齐
setprecision()-定义浮点数的精度
showpoint-强制显示小数点和尾部的0
scientific-科学计数法表示浮点数
fixed-带小数点表示浮点数
setw-设置域宽(只对之后的一个输出有效)
setfil(c)-配合setw使用,将域宽空出来的部分用c填满
showpows-在整数前加+
skipws-忽略输入前的空白
noskipws-不忽略输入前的空白