示例:打印用户输入的数字
#include <iostream>
using namespace std;
int main() {
int number;
cout << "Enter an integer: ";
cin >> number;
cout << "You entered " << number;
return 0;
}
输出
Enter an integer: 23 You entered 23
此程序要求用户输入一个数字。
当用户输入一个整数时,它会使用 注释 存储在变量 number 中。
然后使用 cout 在屏幕上显示。
从这个例子开始,我们将使用以下代码使用 std 命名空间
using namespace std;
这将允许我们分别编写 cout
、cin
、endl
,而不是 std::cout
、std::cin
、std::endl
。
这只是为了让我们的代码更简洁、更易读。
但是,使用 std
命名空间被认为是一种不好的做法,我们强烈建议您在掌握 C++ 的基本知识后放弃这种做法。