ex5_02.cpp

来自「一本语言类编程书籍」· C++ 代码 · 共 30 行

CPP
30
字号
// Exercise 5.2 Summing integers and calculating the average
#include <iostream>
#include <iomanip>
#include <cctype>
using std::cout;
using std::cin;
using std::endl;

int main() {
  char ch = 0;
  int n = 0;
  int count = 0;
  long total = 0L;
  cout << "Enter the the first integer: ";
  while(true) {
   cin >> n;
   total += n;
   ++count;
   cout << "Do you want to enter another(y/n)?";
   cin >> ch;
   if(std::tolower(ch) == 'n')
     break;
   else
     cout << "Enter an integer: ";
  }
  cout << "The total is " << total << endl
    << "The average is " << std::setw(10) << std::setprecision(2)
       << std::fixed << (static_cast<double>(total)/count) << endl;
  return 0;
}

⌨️ 快捷键说明

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