Advertisement
Guest User

Untitled

a guest
Apr 18th, 2015
181
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.42 KB | None | 0 0
  1. // miserable.cpp
  2.  
  3. #include <iostream>
  4. #include <fstream>
  5. #include <iomanip>
  6. using namespace std;
  7.  
  8. int main(){
  9.     int keepGoing = 1, noOfTempValues = 24;
  10.     double tempValue, averageValue, totalValue, max, min;
  11.     char userChoice;
  12.     string fileName = "templog.txt";    //file name
  13.     ifstream inputFile(fileName);   //Opening the file.
  14.     //if opened the file then the program will run
  15.     if (inputFile){
  16.         cout << "\n\nTemperature Statistics\n----------------------\n\nReading logged values for processing and presentation...\n\nPress Enter for menu: ";
  17.         cin.get();  //waiting user to enter input
  18.  
  19.         while (keepGoing){
  20.             system("cls");
  21.             cout << "\n\nMENU\n----\n\n1. Display temperature values\n2. View maximum and minimum temperatures\n3. View average temperature\n4. Quit\n\nMake your choice: ";
  22.             cin.get(userChoice);
  23.             cin.get();
  24.             if (userChoice == '1'){
  25.                 cout << "\nDisplaying the latest 24 temperature values:\n\n";
  26.                 for (int i = 0; i < noOfTempValues; i++){
  27.                     if (i % 6 == 0)
  28.                         cout << endl;
  29.                     inputFile >> tempValue;
  30.                     cout << fixed << setprecision(2) << setw(8) << tempValue;
  31.                 }
  32.             }
  33.  
  34.             else if (userChoice == '2'){
  35.                 cout << "\nCalculating the maximum and minimum temperature...\n";
  36.                 max = 0, min = 0;
  37.                 inputFile >> tempValue;
  38.                 max = min = tempValue;
  39.                 for (int i = 1; i<noOfTempValues; i++){
  40.                     inputFile >> tempValue;
  41.                     if (tempValue>max)
  42.                         max = tempValue;
  43.                     if (tempValue < min)
  44.                         min = tempValue;
  45.                 }
  46.                 cout << "\nMaximum temperature: " << fixed << setprecision(2) << max << " degrees Celcius\n";
  47.                 cout << "\nMinimum temperature: " << min << " degrees Celcius\n";
  48.             }
  49.  
  50.             else if (userChoice == '3'){
  51.                 cout << "\nCalculating average temperature...\n";
  52.                 totalValue = 0.0;
  53.                 for (int i = 0; i < noOfTempValues; i++){
  54.                     inputFile >> tempValue;
  55.                     totalValue += tempValue;
  56.                 }
  57.                 averageValue = totalValue / 24;
  58.                 cout << "\nAverage temperature: ";
  59.                 cout << fixed << setprecision(2) << averageValue << " degrees Celcius\n";
  60.             }
  61.  
  62.             else{
  63.                 keepGoing = 0;
  64.                 cout << "\n\nTerminating the program.";
  65.             }
  66.             cout << "\n\nPress Enter to continue:";
  67.             cin.get();
  68.             //this line to start reading from the beginning of the file.
  69.             inputFile.clear();
  70.             inputFile.seekg(0, ios::beg);
  71.         }
  72.         inputFile.close();
  73.     }
  74.     //if the file didn't open
  75.     else
  76.         cerr << "File could not be opened!\n"; // Report error
  77.     return 0;
  78. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement