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

📄 ex5_02.cpp

📁 一本语言类编程书籍
💻 CPP
字号:
// 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 + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -