Question #37447

Consider a text file named “Data.txt” which contains an unknown number of positive integers. Write a C++ program which reads the numbers from the file and display their total and maximum on the screen. The program should stop when one or more of the conditions given below become true:
1. The total has exceeded 5555.
2. The end of file has been reached.

Expert's answer

#include <iostream> #include <fstream>using namespace std;int main(int argc, char const *argv[])  {         int total   = 0,                    max     = 0;      ifstream input;      input.open ("Data.txt");      while ((!input.eof()) && (total < 5555))          {                     int temp;             input >> temp;             total += temp;             if (temp > max)                               max = temp;         }    input.close ();    cout << "Total: " << total << endl;        cout << "Maximum: " << max << endl;       return 0; }
LATEST TUTORIALS
APPROVED BY CLIENTS