mardi 24 mars 2015

How would I store data I have inputted into a txt file to arrays?

I am creating a program that acts as a payroll system in which I create a file name and enter in: ID (int), pay (double), hours (int) and gross wage (double). How would I take the data I enter to the file and put them in arrays and display them. Later on, I will need to sort through them as I have cases for in my switch statement.



#include <fstream>

#include <iostream>

#include <iomanip>

#include <string>



using namespace std;



//Function prototypes

void getEmployeeInfo(int&, double&, int&); //EmployeeInfo prototype

double calcWage(double, int, double&); //calcWage prototype

void printWages(ofstream &, int, double, int , double); //print wages prototype



//Main function

int main(){



int ID, hours, caseInput;

double pay, gross;

char input;



ofstream outputFile;

string filename;







do {

cout << " Menu " << endl;

cout << "1. Calculate gross wages for employees" << endl;

cout << "2. Display employees information to screen" << endl;

cout << "3. Display information in order of ID" << endl;

cout << "4. Display information in order of hourly rate" << endl;

cout << "5. Display information in order of hours worked" << endl;

cout << "6. Display information in order of wage" << endl;

cout << "7. Quit the system" << endl;



cout << "Enter your option --> ";

cin >> caseInput;





switch (caseInput) {



case 1: //Create file

cout << "Enter the filename: ";

cin >> filename;



//open file

outputFile.open(filename.c_str());

outputFile << setw(10) << "ID" << setw (15) << "Pay" << setw(17) << "Hours"

<< setw(11) << "Gross"<<endl;

outputFile << "---------------------------------------------------------\n";



do{

getEmployeeInfo(ID, pay, hours);

calcWage(pay, hours, gross);

printWages(outputFile, ID, pay, hours, gross);

cout << "Do you want to enter another employee's information? ";

cin >> input;

}

while(input == 'y' || input == 'Y');



//If user does not enter y, close file

outputFile.close();

cout << "The result is reported to the file " <<

filename << "." << endl;

break;



case 2:

break;



case 3:

break;



case 4:

break;



case 5: cout << "Thank you for using Math Tutor." << endl;

break;



case 6: cout << "Thank you for using Math Tutor." << endl;

break;



case 7: cout << "Thank you for using the Payroll System." << endl;

break;



default: cout << "Error. Enter a number 1-7." << endl;

}

}

while(caseInput != 7);





return 0;

}



//Function to input employee info

void getEmployeeInfo(int &ID, double &pay, int &hours){



cout << "Enter an employee's information by the order of ID number, rate, hours: ";

cin >> ID >> pay >> hours;



while(ID < 0){

cout << "You must enter a non negative value. Try again!" << endl;

cin >> ID;

}



while(pay < 0){

cout << "You must enter a non negative value. Try again!" << endl;

cin >> pay;

}



while(hours < 0){

cout << "You must enter a non negative value. Try again!" << endl;

cin >> hours;

}



}



//Function calculates gross pay

double calcWage(double pay, int hours, double &gross){



gross = pay * hours;

return gross;



}



//Print function

void printWages(ofstream &outputFile, int ID, double pay, int hours, double gross){



outputFile << setw(10)<< ID << setw(12) << "$" << setprecision(2)

<< fixed << pay << setw(13) << hours

<< setw(10) << "$" << fixed << gross << endl;





}

Aucun commentaire:

Enregistrer un commentaire