Midterm Najia

mail@pastecode.io avatar
unknown
csharp
2 months ago
4.8 kB
0
Indexable
Never
#include <iostream>
#include <fstream>
#include <iomanip>
#include <sstream>

using namespace std;

const string FILENAME = "temps.txt";

// Function to calculate averages and display the results
void calculateAndDisplayAverages(double low, double high, ofstream &outfile) {
    double avgCelsius = (low + high) / 2.0;
    double avgFahrenheit = avgCelsius * 9 / 5 + 32;
    double avgKelvin = avgCelsius + 273.15;

    // Display averages in console
    cout << fixed << setprecision(1);
    cout << setw(8) << low << setw(12) << high << setw(12) << avgCelsius << setw(12) << avgFahrenheit << setw(12) << avgKelvin << endl;

    // Write entry to file
    outfile << low << "\t" << high << endl;
}

int main() {
    ifstream infile(FILENAME);
    ofstream outfile(FILENAME, ios::app);

    // Check if the file exists
    if (!infile.is_open()) {
        cout << "Unable to open input file! Try again." << endl;
        // Get input for the first entry
        double firstLow, firstHigh;
        cout << "Enter low temp: ";
        cin >> firstLow;
        cout << "Enter high temp: ";
        cin >> firstHigh;

        // Validate input
        while (firstLow <= 0) {
            cout << "Low temp must be greater than zero. Please try again." << endl;
            cout << "Enter low temp: ";
            cin >> firstLow;
        }

        while (firstHigh <= 0) {
            cout << "High temp must be greater than zero. Please try again." << endl;
            cout << "Enter high temp: ";
            cin >> firstHigh;
        }

        // Calculate and display averages for the first entry
        calculateAndDisplayAverages(firstLow, firstHigh, outfile);
    } else {
        // File exists, read existing entries and display averages
        double totalLow = 0.0, totalHigh = 0.0;
        double entryCount = 0;

        // Display header
        cout << setw(8) << "Low" << setw(12) << "High" << setw(12) << "Avg(C)" << setw(12) << "Avg(F)" << setw(12) << "Avg(K)" << endl;

        while (infile >> ws) {
            double low, high;
            infile >> low >> high;

            // Validate unexpected data
            if (infile.fail()) {
                cerr << "Error reading data from file." << endl;
                return 1;
            }

            calculateAndDisplayAverages(low, high, outfile);

            // Update total for averaging
            totalLow += low;
            totalHigh += high;
            entryCount++;
        }

        // Calculate and display total averages
        double totalAvgCelsius = totalLow + totalHigh / (2 * entryCount);
        double totalAvgFahrenheit = totalAvgCelsius * 9 / 5 + 32;
        double totalAvgKelvin = totalAvgCelsius + 273.15;

        cout << fixed << setprecision(2);
        cout << "Total Avg(C): " << totalAvgCelsius << endl;
        cout << "Total Avg(F): " << totalAvgFahrenheit << endl;
        cout << "Total Avg(K): " << totalAvgKelvin << endl;

        // Get input for the new entry
        double newLow, newHigh;
        cout << "Enter low temp: ";
        cin >> newLow;

        // Validate input
        while (newLow <= 0) {
            cout << "Low temp must be greater than zero. Please try again." << endl;
            cout << "Enter low temp: ";
            cin >> newLow;
        }

        cout << "Enter high temp: ";
        cin >> newHigh;

        // Validate input
        while (newHigh <= 0) {
            cout << "High temp must be greater than zero. Please try again." << endl;
            cout << "Enter high temp: ";
            cin >> newHigh;
        }

        // Calculate and display averages for the new entry
        calculateAndDisplayAverages(newLow, newHigh, outfile);

        // Calculate and display updated total averages
        totalLow += newLow;
        totalHigh += newHigh;
        entryCount++;

        totalAvgCelsius = totalLow + totalHigh / (2 * entryCount);
        totalAvgFahrenheit = totalAvgCelsius * 9 / 5 + 32;
        totalAvgKelvin = totalAvgCelsius + 273.15;

        cout << fixed << setprecision(2);
        cout << "Total Avg(C): " << totalAvgCelsius << endl;
        cout << "Total Avg(F): " << totalAvgFahrenheit << endl;
        cout << "Total Avg(K): " << totalAvgKelvin << endl;
    }

    char repeat;
    cout << "Press y for another trip or any other non-whitespace key to quit: ";
    cin >> repeat;

    while (repeat == 'y' || repeat == 'Y') {
        // Continue the loop with the same logic as above

        // ...

        cout << "Press y for another trip or any other non-whitespace key to quit: ";
        cin >> repeat;
    }

    cout << "Bye!" << endl;

    return 0;
}
Leave a Comment