https://rentry.org/PPP2_p154

// Code derived from Stroustrup's PPP2 book
// § 5.7 Logic errors
//  -and beginning on p 154

#include <iostream>
#include <vector>

using std::cin;
using std::cout;
using std::vector;

int main()
{
  cout << "Please enter temperatures: (enter '|' to stop):\n";
  vector<double> temps;            // temperatures
  for (double temp; cin >> temp;)  // read and put into temps
    temps.push_back(temp);         //

  double sum       = 0;
  double high_temp = 0;
  double low_temp  = 0;

  for (int x : temps) {
    if (x > high_temp)  // record high
      high_temp = x;    //
                        //
    if (x < low_temp)   // record low
      low_temp = x;     //

    sum += x;  // compute sum
  }

  cout << "High temperature: " << high_temp << '\n';
  cout << "Low temperature: " << low_temp << '\n';
  cout << "Average temperature: " << sum / temps.size() << '\n';
}

build & run:

g++ -std=c++20 -O2 -Wall -pedantic ./ch_05/main_p154.cpp && ./a.out

PrevUpNext

Edit
Pub: 24 Feb 2023 10:39 UTC
Edit: 29 Apr 2023 09:48 UTC
Views: 584