使用cin的返回值获取C ++中未知数量的输入
有时我们的程序中需要可变数量的输入。在该程序中,我们将看到如何使用cin来获取可变数量的输入。
一种简单的解决方案是运行一个循环,当按下一个特定值时,它将停止。另一个想法是使用cin>>输入。当值是非数字时,它将返回false。
示例
#include <iostream>
using namespace std;
main() {
int input;
int n = 0;
cout << "Enter numbers. (To stop enter any character): ";
while (cin >> input)
n++;
cout << "\nTotal number of inputs entered: " << n;
}输出结果
Enter numbers. (To stop enter any character): 5 6 7 2 6 5 j Total number of inputs entered: 6
热门推荐
10 新人红包祝福语简短精辟
11 幼师给毕业祝福语简短
12 生日高考祝福语妹妹简短
13 下飞机祝福语简短英文
14 元宵发客户祝福语简短
15 爸妈金婚文案祝福语简短
16 新郎朋友红包祝福语简短
17 虎年五一祝福语大全简短
18 出阁宴祝福语姐姐简短