[C++之旅] 2 基本输入、输出
cin和cout为预定义对象,cin主要用于从终端的键盘读取数据 ,cout则知道如何显示字符串、数字、和字符等,cin;endl是一个特殊的C++符号,表示一个重要的概念:重起一行,符号<<和>>为重载流插入运算符和流提取运算符,被选择用来指示信息流的方向;输出时,<<运算符将字符串插入到输出流中;输入时,cin使用>>运算符从输入流中抽取字符。
程序清单:
//1.1 cin_cout
//input and output
#include <iostream>
int main()
{
using std::cout; //make cout available
using std::endl; //make endl available
using std::cin; //make sin available
int apples; //declare an integer variable
apples = 0; //assign a value to the variable
cout << "How many apples do you have?" << endl;
cin >> apples; //C++ input
cout << "Here are two more.";
apples += 2; //modify the value of the variable
cout << "Now you have "
<< apples
<< " apples."
<< endl;
return 0;
}
运行->键盘输入15。
运行结果:
How many apples do you have?
15
Here are two more.Now you have 17 apples.
网友评论