在C语言中,我们通常会使用 scanf 和 printf 来对数据进行输入输出操作。在C++语言中,C语言的这一套输入输出库我们仍然能使用,但是 C++ 又增加了一套新的、更容易使用的输入输出库。
【例1】简单的输入输出代码示例:
#include
using namespace std;
int main(){
int x;
float y;
cout<<“Please input an int number:”<<endl;
cin>>x;
cout<<"The int number is x= "<<x<<endl;
cout<<“Please input a float number:”<<endl;
cin>>y;
cout<<"The float number is y= "<<y<<endl;
return 0;
}
运行结果如下(↙表示按下回车键):
Please input an int number:
8↙
The int number is x= 8
Please input a float number:
7.4↙
The float number is y= 7.4
C++ 中的输入与输出可以看做是一连串的数据流,输入即可视为从文件或键盘中输入程序中的一串数据流,而输出则可以视为从程序中输出一连串的数据流到显示屏或文件中。
在编写 C++ 程序时,如果需要使用输入输出时,则需要包含头文件iostream,