02 输入cin 输出cout

发布时间 2024-01-02 12:24:38作者: 奔跑的窜天熊

从一个简单的输入输出入手:

输入年龄,输出年龄。

#include<iostream>

using namespace std;

 int main() {

int age;

cout << "please input your age:";

cout << "\n";

cin >> age;

cout << "your age is " << age;

return 0;

}

cout和cin的用法:将cout 记成屏幕内容沿箭头传递过去;cin是键盘输入,输入就沿着箭头传递给变量。

输出想实现换行可以使用:

  1)上面的写法cout<<“\n”;

  2)也可以用 cout<< endl;     这个endl 就是endline结束这一行

 3)还可以采用连着写

 

 

是不是说endl只能写在最后呢?


不是的,endl只是提供一个换行,可以在任意位置进行写入,以hello world为例:

 

 

 

#include<iostream>using namespace std;
 int main() {int age;cout << "please input your age:";cout << "\n";cin >> age;cout << "your age is " << age;

return 0;}