注意:本随笔是直接参考《C++Primer(第四版)习题解答(完整版)》中的。此处主要是便于本人以后反复阅读。
习题1.编写程序,要求用户输入一组数。输出信息说明其中有多少个负数。
【解答】
1 #include <iostream> 2 using namespace std; 3 4 int main() 5 { 6 int value,amount = 0; 7 cout << "Enter values" << endl; 8 cout << "输入文件结束符‘/0’可以终止输入" << endl; 9 10 while (cin>>value) 11 { 12 if (value<0) 13 { 14 ++amount; 15 } 16 } 17 cout <<"amount of all negative values read is "<< amount<<endl; 18 return 0; 19 }
Enter values
输入文件结束符‘/0’可以终止输入
1
3
4
5
5
0
-3
-5
/0
amount of all negative values read is 2
请按任意键继续. . .
原文:http://www.cnblogs.com/haihai187/p/6539478.html