Untitled

 avatar
unknown
plain_text
3 months ago
6.3 kB
7
Indexable
#include <iostream>
#include <fstream>
#include <vector>
#include <iomanip>
#include <cstring>

using namespace std;

// Structure for MPG Calculation
struct MPG {
    char carName[50];
    char tripName[50];
    double milesDriven;
    double gasUsed;
    double mpg;
};

// Structure for Simple Pay Calculation
struct Pay {
    char employeeName[50];
    double hoursWorked;
    double hourlyRate;
    double totalPay;
};

// Vectors to store calculations
vector<MPG> mpgRecords;
vector<Pay> payRecords;

// Function to calculate MPG
void calculateMPG() {
    MPG mpgEntry;
    cout << "Enter car name: ";
    cin.ignore();
    cin.getline(mpgEntry.carName, 50);
    cout << "Enter trip name: ";
    cin.getline(mpgEntry.tripName, 50);
    cout << "Enter miles driven: ";
    cin >> mpgEntry.milesDriven;
    cout << "Enter gallons of gas used: ";
    cin >> mpgEntry.gasUsed;

    if (mpgEntry.gasUsed == 0) {
        cout << "Error: Gas used cannot be zero!" << endl;
        return;
    }

    mpgEntry.mpg = mpgEntry.milesDriven / mpgEntry.gasUsed;
    mpgRecords.push_back(mpgEntry);

    cout << "\nMPG Calculation Saved:\n";
    cout << mpgEntry.carName << " - " << mpgEntry.tripName
         << " Miles = " << mpgEntry.milesDriven
         << " Gallons = " << mpgEntry.gasUsed
         << " MPG = " << fixed << setprecision(2) << mpgEntry.mpg << endl;
}

// Function to calculate Pay
void calculatePay() {
    Pay payEntry;
    cout << "Enter employee name: ";
    cin.ignore();
    cin.getline(payEntry.employeeName, 50);
    cout << "Enter hours worked: ";
    cin >> payEntry.hoursWorked;
    cout << "Enter hourly rate: ";
    cin >> payEntry.hourlyRate;

    payEntry.totalPay = payEntry.hoursWorked * payEntry.hourlyRate;
    payRecords.push_back(payEntry);

    cout << "\nPay Calculation Saved:\n";
    cout << payEntry.employeeName << " - Hours Worked: " << payEntry.hoursWorked
         << " @ $" << fixed << setprecision(2) << payEntry.hourlyRate
         << " = $" << payEntry.totalPay << endl;
}

// Function to display MPG records
void viewMPGCalculations() {
    if (mpgRecords.empty()) {
        cout << "No MPG records found.\n";
        return;
    }
    cout << "\nMPG Calculations:\n";
    for (const auto &mpg : mpgRecords) {
        cout << mpg.carName << " - " << mpg.tripName
             << " Miles = " << mpg.milesDriven
             << " Gallons = " << mpg.gasUsed
             << " MPG = " << fixed << setprecision(2) << mpg.mpg << endl;
    }
}

// Function to display Pay records
void viewPayCalculations() {
    if (payRecords.empty()) {
        cout << "No Pay records found.\n";
        return;
    }
    cout << "\nPay Calculations:\n";
    for (const auto &pay : payRecords) {
        cout << pay.employeeName << " - Hours Worked: " << pay.hoursWorked
             << " @ $" << fixed << setprecision(2) << pay.hourlyRate
             << " = $" << pay.totalPay << endl;
    }
}

// Function to save MPG records to a file
void saveMPGToFile() {
    char filename[50];
    cout << "Enter filename to save MPG records: ";
    cin.ignore();
    cin.getline(filename, 50);

    ofstream file(filename);
    if (!file) {
        cout << "Error opening file!\n";
        return;
    }

    for (const auto &mpg : mpgRecords) {
        file << mpg.carName << "," << mpg.tripName << ","
             << mpg.milesDriven << "," << mpg.gasUsed << "," << mpg.mpg << endl;
    }
    file.close();
    cout << "MPG records saved to " << filename << endl;
}

// Function to save Pay records to a file
void savePayToFile() {
    char filename[50];
    cout << "Enter filename to save Pay records: ";
    cin.ignore();
    cin.getline(filename, 50);

    ofstream file(filename);
    if (!file) {
        cout << "Error opening file!\n";
        return;
    }

    for (const auto &pay : payRecords) {
        file << pay.employeeName << "," << pay.hoursWorked << ","
             << pay.hourlyRate << "," << pay.totalPay << endl;
    }
    file.close();
    cout << "Pay records saved to " << filename << endl;
}

// Function to view MPG records from a file
void viewMPGFile() {
    char filename[50];
    cout << "Enter MPG filename to view: ";
    cin.ignore();
    cin.getline(filename, 50);

    ifstream file(filename);
    if (!file) {
        cout << "Error opening file!\n";
        return;
    }

    cout << "\nMPG Records from File:\n";
    string line;
    while (getline(file, line)) {
        cout << line << endl;
    }
    file.close();
}

// Function to view Pay records from a file
void viewPayFile() {
    char filename[50];
    cout << "Enter Pay filename to view: ";
    cin.ignore();
    cin.getline(filename, 50);

    ifstream file(filename);
    if (!file) {
        cout << "Error opening file!\n";
        return;
    }

    cout << "\nPay Records from File:\n";
    string line;
    while (getline(file, line)) {
        cout << line << endl;
    }
    file.close();
}

// Main menu
void menu() {
    int choice;
    do {
        cout << "\n===== Menu =====\n";
        cout << "1. Calculate MPG\n";
        cout << "2. Calculate Simple Pay\n";
        cout << "3. View MPG Calculations\n";
        cout << "4. View Pay Calculations\n";
        cout << "5. Save MPG to File\n";
        cout << "6. Save Pay Calculations to File\n";
        cout << "7. View MPG File\n";
        cout << "8. View Pay File\n";
        cout << "9. Exit\n";
        cout << "Enter choice: ";
        cin >> choice;

        switch (choice) {
            case 1: calculateMPG(); break;
            case 2: calculatePay(); break;
            case 3: viewMPGCalculations(); break;
            case 4: viewPayCalculations(); break;
            case 5: saveMPGToFile(); break;
            case 6: savePayToFile(); break;
            case 7: viewMPGFile(); break;
            case 8: viewPayFile(); break;
            case 9: cout << "Exiting...\n"; break;
            default: cout << "Invalid choice, try again.\n";
        }
    } while (choice != 9);
}

// Main function
int main() {
    menu();
    return 0;
}
Editor is loading...
Leave a Comment