Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // miserable.cpp
- #include <iostream>
- #include <fstream>
- #include <iomanip>
- using namespace std;
- int main(){
- int keepGoing = 1, noOfTempValues = 24;
- double tempValue, averageValue, totalValue, max, min;
- char userChoice;
- string fileName = "templog.txt"; //file name
- ifstream inputFile(fileName); //Opening the file.
- //if opened the file then the program will run
- if (inputFile){
- cout << "\n\nTemperature Statistics\n----------------------\n\nReading logged values for processing and presentation...\n\nPress Enter for menu: ";
- cin.get(); //waiting user to enter input
- while (keepGoing){
- system("cls");
- 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: ";
- cin.get(userChoice);
- cin.get();
- if (userChoice == '1'){
- cout << "\nDisplaying the latest 24 temperature values:\n\n";
- for (int i = 0; i < noOfTempValues; i++){
- if (i % 6 == 0)
- cout << endl;
- inputFile >> tempValue;
- cout << fixed << setprecision(2) << setw(8) << tempValue;
- }
- }
- else if (userChoice == '2'){
- cout << "\nCalculating the maximum and minimum temperature...\n";
- max = 0, min = 0;
- inputFile >> tempValue;
- max = min = tempValue;
- for (int i = 1; i<noOfTempValues; i++){
- inputFile >> tempValue;
- if (tempValue>max)
- max = tempValue;
- if (tempValue < min)
- min = tempValue;
- }
- cout << "\nMaximum temperature: " << fixed << setprecision(2) << max << " degrees Celcius\n";
- cout << "\nMinimum temperature: " << min << " degrees Celcius\n";
- }
- else if (userChoice == '3'){
- cout << "\nCalculating average temperature...\n";
- totalValue = 0.0;
- for (int i = 0; i < noOfTempValues; i++){
- inputFile >> tempValue;
- totalValue += tempValue;
- }
- averageValue = totalValue / 24;
- cout << "\nAverage temperature: ";
- cout << fixed << setprecision(2) << averageValue << " degrees Celcius\n";
- }
- else{
- keepGoing = 0;
- cout << "\n\nTerminating the program.";
- }
- cout << "\n\nPress Enter to continue:";
- cin.get();
- //this line to start reading from the beginning of the file.
- inputFile.clear();
- inputFile.seekg(0, ios::beg);
- }
- inputFile.close();
- }
- //if the file didn't open
- else
- cerr << "File could not be opened!\n"; // Report error
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement