但是,如何从终端获得用户输入?或者,如何将文件中的数据输入到程序中或将结果写入文件?
cin
标准库提供输出到终端的功能。同样地,库还提供从终端读入数据的功能。
这段代码演示了如何使用 cin:
# include <iostream> # include <vector> using namespace std; int main() { int integerone; int integertwo; //声明数组并赋值 cout << "Enter an integer between 1 and 100" << endl; cin >> integerone; cout << "Enter another integer between 1 and 100" << endl; cin >> integertwo; //输出差 cout << "The difference between your two numbers is: "; cout << integerone - integertwo << endl; return 0; }