mercredi 30 octobre 2019

How can I find and display something in a data file?

#include <iostream>
#include <fstream>
#include <conio.h>
#include <iomanip>
#include <math.h>
#include <stdlib.h>

using namespace std;


int main()
{



int option, recordnum, count = 0;
float grosspay, hours, payrate, taxes, taxrate, netpay, taxp, payamount;
char firstname[10], lastname[10], employid[10], again = 'y';

do
{
    system("cls");
    cout << "Enter An Option.\n";
    cout << "1. Write new Records to the Data File.\n";
    cout << "2. Display Records from the Data File.\n";
    cout << "3. Find all records with a pay rate less than $ N dollar per hour.\n";
    cout << "4. Exit\n";
    cout << "What would you like to do?: ";
    cin >> option;
    option = fabs(option);

    if (option < 1 or option > 4)
    {
        system("cls");
        cout << "Enter An Option.\n";
        cout << "1. Write new Records to the Data File.\n";
        cout << "2. Display Records from the Data File.\n";
        cout << "3. Find all records with a pay rate less than $ N dollar per hour.\n";
        cout << "4. Exit\n";
        cout << "What would you like to do?: " << "\n";
        cin >> option;
    }

    system("cls");
    switch (option)
    {
    case 1:
    {   
        ofstream outFile("personnel.dat", ios::out | ios::app);
        outFile.precision(2);
        outFile.setf(ios::fixed);

        cout << "How Many Records Do You Want to Enter?: ";
        cin >> recordnum;
        recordnum = abs(recordnum);

        if (recordnum == 0)
            break;

        for (int count = 1; count <= recordnum; count++)
        {
            system("cls");
            cout << "Enter Employee's First Name: ";
            cin >> firstname;
            cout << "Enter Employee's Last Name: ";
            cin >> lastname;
            cout << "Enter Employee's ID #: ";
            cin >> employid;
            cout << "Enter Employee's Hours Worked: ";
            cin >> hours;
            hours = fabs(hours);
            cout << "Enter Employee's Pay Rate: ";
            cin >> payrate;
            payrate = fabs(payrate);
            cout << "Enter Employee's Tax Rate: ";
            cin >> taxrate;
            taxrate = fabs(taxrate);

            grosspay = hours * payrate;
            taxes = grosspay * taxrate;
            netpay = grosspay * taxes;

            outFile << firstname << " " << lastname << " " << employid << " " << hours << " " << payrate << " " << taxrate << " " << grosspay << " " << taxes << " " << netpay << " " << "\n";
        }
        outFile.close();
        break;
    }
    case 2:
    {
        ifstream inFile("personnel.dat", ios::in);
        inFile >> firstname >> lastname >> employid >> hours >> payrate >> taxrate >> grosspay >> taxes >> netpay;

        while (!inFile.eof())
        {
            count++;
            taxp = taxrate * 100;
            cout << "\n\n Record Number: " << count;
            cout << "\n\n First Name: " << firstname;
            cout << "\n\n Last Name: " << lastname;
            cout << "\n\n Employee's ID #: " << employid;
            cout << "\n\n Hours Worked: " << hours;
            cout << "\n\n Pay Rate: " << payrate;
            cout << "\n\n Gross Pay: $" << grosspay;
            cout << "\n\n Taxes: " << taxes;
            cout << "\n\n Net Pay: $" << netpay;
            cout << "\n\n Tax Rate: " << taxp << "%";
            inFile >> firstname >> lastname >> employid >> hours >> payrate >> taxrate >> grosspay >> taxes >> netpay;
        }

        cout << "\n\n Total Numbers of Files: " << count;
        inFile.close();
        count = 0;
        cout << "\n\n Press Any Keys to Continue...";
        getch();
        break;
    }
    case 3:
    {
        cout << "Enter the Pay Rate amount to Find All Employee's Pay Rate Less than: ";
        cin >> payamount;

        for (payamount = payamount; payamount <= payamount; payamount++)
        {

        }
    }
} while (again == 'y');

}

So for case 3, I have to find the pay amount that is less than the pay amount inputted and display it. But i also have to display everything along with that pay amount, ie the first name, last name, empid, etc. Would i need to do another inFile along with a for loop? or would I have to do something else?

Aucun commentaire:

Enregistrer un commentaire