Using namespace std;
1 cout << "enter two numbers:" << endl; 2 int v1 = 0, v2 = 0; 3 cin >> v1 >> v2; 4 cout << "the sum of " << v1 << " and " << v2 << " is " << v1 + v2 << endl;
# the sum of 4 and 6 is 10
#输出变量v1,v2两边加空格,这样控制台输出结果才好看
#空格和回车 输入多个数
1 while (cin>>value) 2 { 3 sum += value; 4 } 输入任意字母,退出循环,控制台得出结果
这里只统计最后连续输出的值
int v = 0,a,cnt; while (cin>>a) { if (a == v) { ++cnt; } else { cnt = 1; v = a; } } cout << v << " 出现了 " << cnt << " 次 " << endl;