⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 soln3_1a.cpp

📁 Wrox.Ivor.Hortons.Beginning.Visual.C.Plus.Plus.2008 With sourcecode
💻 CPP
字号:
// Soln3_1A.cpp
/* Note that val is not initialized in the code and so will contain a spurious value 
   at the beginning. That's why you read the first value for val before the loop.
   If you initialize val to 0 then you can do all the input inside the loop:

      int val=0, total=0;
   cout << "Enter numbers, one per line. Enter 0 to end:\n";

   while (val != 0)
   {
     cin >> val;
     total += val;
   }

   Now you read val in the loop before adding the value to total.
*/

#include <iostream>
using std::cin;
using std::cout;
using std::endl;

int main()
{
   int val, total=0;
   cout << "Enter numbers, one per line. Enter 0 to end:\n";
   cin >> val;

   while (val != 0)
   {
     total += val;
     cin >> val;
   }

   cout << "\nThank you. The total was " << total;
   cout << endl;
   return 0;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -