Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

C++ Programming Project: File I/O I wrote this program incorrectly. I would like it to be corrected to understand where I went wrong. The feedback

C++ Programming Project: File I/O

I wrote this program incorrectly. I would like it to be corrected to understand where I went wrong. The feedback I received was that "read() does not return an employee and main() does not read employee data from file -- it uses objects created for output."

Below is my code, along with the original program instructions.

-------------------------------------------------------------

The Employee Class

For this project you need to add the following member functions to your Employee class.

Employee read(ifstream&) void write(ofstream&) 

The function Employee::read is a static member function. It returns an Employee object based on the data it reads from a file. It must be called as Employee::read(...) since it's job is to create an object. If there is a read error, it throws a std::runtime_error (defined in ) exception with an appropriate message. Note that you are passing the stream by reference, not by value (streams cannot be copied).

The main() function

The major goal of main() is to test the additions to your Employee class. Use the printCheck() function from your previous Employee project. The output of your program will look similar to the output of your previous Employee project.

Your driver will contain a main() function that does the following:

Present the user with a menu of choices, create a data file, or read data from a file and print checks:

This program has two options: 1 - Create a data file, or 2 - Read data from a file and print paychecks. Please enter <1> to create a file or <2> to print checks: 

If the user selects the first option, your program should:

Create an ofstream object using a file name obtained from the user. Pass just the file name as the parameter (no path) so that your program assumes the file to be in the same folder as your executable file.

Create three employee objects as shown:

Employee joe(37, "Joe Brown", "123 Main St.", "123-6788", 45.00, 10.00)

Employee sam(21, "Sam Jones", "45 East State", "661-9000", 30.00, 12.00)

Employee mary(15, "Mary Smith", "12 High Street", "401-8900", 40.00, 15.00)

Send messages to each of the three Employee objects to write themselves out to the file.

Print a message that creation of the file is complete.

Exit.

If the user selects the second option, your program should:

Prompt for the name of the file that the program saved.

Call Employee::read to read in the objects written in step 1, one-by-one.

Call the printCheck() function for each of the three new objects, just as you did in the previous project.

Exit.

Run the second option twice. Once with the correct filename, and once with an incorrect one. The second run will test your exception handling. In the error case, throw a std::runtime_error (defined in ) with a suitable error message string. Catch the exception and print its message in main, then exit the program. Note: you must test for input errors for each input operation.

In addition, when reading the file for the second part, you need to check that the expected data is there. If there are any errors in the data (there shouldn't be, but you should check anyway), your code should throw a std::runtime_error with a message explaining which data was corrupted.

Submitting Your Assignment

Submit your 3 source files, just as you did in the previous project.

Sample execution follows. Note that only the checks are printed.

This program has two options: 1 - Create a data file, or 2 - Read data from a file and print paychecks. Please enter (1) to create a file or (2) to print checks:1 Please enter a file name: employee.dat Data file created ... you can now run option 2. This program has two options: 1 - Create a data file, or 2 - Read data from a file and print paychecks. Please enter (1) to create a file or (2) to print checks:2 Please enter a file name: employee.dat ....................UVU Computer Science Dept................................. Pay to the order of Joe Brown....................................$344.38 United Community Credit Union .............................................................................. Hours worked: 45.00 Hourly wage: 10.00 ....................UVU Computer Science Dept................................. Pay to the order of Sam Jones....................................$261.00 United Community Credit Union .............................................................................. Hours worked: 30.00 Hourly wage: 12.00 ....................UVU Computer Science Dept................................. Pay to the order of Mary Smith....................................$435.00 United Community Credit Union .............................................................................. Hours worked: 40.00 Hourly wage: 15.00 This program has two options: 1 - Create a data file, or 2 - Read data from a file and print paychecks. Please enter (1) to create a file or (2) to print checks:2 Please enter a file name: foo.dat Couldn't open file for input 

-----------------------------------------------------------------------------------------------------------------------------

//main.cpp

#include #include #include "employee.h" using namespace std;

int main() { int userInput; cout.setf(ios::fixed); cout.setf(ios::showpoint); cout.precision(2); Employee e; ifstream inputFile; ofstream outputFile; char filename[100];

cout << "This program has two options:" << endl; cout << "1 - Create a data file, or " << endl; cout << "2 - Read data from a file and print paychecks." << endl; cout << "Please enter (1) to create a file or (2) to print checks: ";

cin >> userInput; cout << endl << endl;

if (userInput == 1) {

cout << "Please enter a file name: "; cin >> filename; cout << endl << endl; if(outputFile) { outputFile.open(filename,std::ios_base::app); Employee joe(37, "Joe Brown", "123-6788","123 Main St.", 10.00,45.00); joe.write(outputFile); Employee sam(21, "Sam Jones", "661-9000","45 East State", 12.00, 30.00); sam.write(outputFile); Employee mary(15, "Mary Smith", "401-8900","12 High Street", 15.00, 40.00); mary.write(outputFile); outputFile.close(); cout << "Data file created ... you can now run option 2." << endl << endl << endl; } else { throw std::runtime_error("Couldn't create file."); } }

else if (userInput == 2) { cout << "Please enter a file name: "; cin >> filename; cout << endl << endl; if(inputFile) { inputFile.open(filename); e.read(inputFile); inputFile.close(); } else { throw std::runtime_error("Couldn't open file for input"); } }

else { cout << "Invalid input" << endl; }

return 0; }

--------------------------------------------------------------------------------------------

//employee.cpp

#include #include #include #include using namespace std; #include "employee.h"

Employee::Employee(void) { employeeNumber = 0; name = "none"; phone = "none"; address = "none"; hourlyWage = 0; hoursWorked = 0; }

Employee::Employee(int empNum,string empName, string empPhone, string empAddress, double wage, double hours) { employeeNumber = empNum; name = empName; phone = empPhone; address = empAddress; hourlyWage = wage; hoursWorked = hours; }

int Employee::getEmployeeNumber() { return employeeNumber; } string Employee::getName() { return name; } string Employee::getAddress() { return address; } string Employee::getPhone() { return phone; } double Employee::getHourlyWage() { return hourlyWage; } double Employee::getHourlyWorked() { return hoursWorked; }

void Employee::setName(string empName) { name = empName; } void Employee::setAddress(string empAddress) { address = empAddress; } void Employee::setPhone(string empPhone) { phone = empPhone; } void Employee::setHourlyWaged(double wage) { hourlyWage = wage; } void Employee::setHourlyWorked(double hours) { hoursWorked = hours; }

double Employee::calcPay() { double pay = hoursWorked * hourlyWage; double fedTax = 0.20; double stateTax = 0.075; double overtime = 1.5; int overtimeHours = hoursWorked - 40; if (hoursWorked > 40) { double grossPay = (40 * hourlyWage) + (overtimeHours * (hourlyWage *overtime)); pay = grossPay - ((grossPay * fedTax) + (grossPay * stateTax)); return pay; } else { pay = pay - ((pay * 0.2) + (pay * 0.075)); } return pay; }

void Employee::printCheck() { const string DASH1 = ".........."; const string DASH2 = "............"; const string DASH3 = "......"; const string DASH4 = "..........."; const string COMPANY = "UVU Computer Science Dept"; const string PAY = "Pay to the order of "; const string BANK = "United Community Credit Union"; cout << DASH1 << DASH1 << COMPANY << DASH4 << DASH4 << DASH4; cout << endl << endl << endl; cout << PAY << getName() << DASH2 << DASH2 << DASH2 << "$" << calcPay(); cout << endl << endl << endl; cout << BANK << endl; cout << DASH2 << DASH2 << DASH2 << DASH2 << DASH2 << DASH2 << DASH3; cout << endl << endl; cout << "Hours worked: " << getHourlyWorked() << endl; cout << "Hourly wage: " << getHourlyWage(); cout << endl << endl << endl << endl << endl << endl; }

Employee Employee::read(ifstream& inputFile) { Employee e; if(!inputFile) { cout<<"Error: file not found"; return e; } int id; int i = 0; string name; string phone; string address; string line; string data; string dt[10]; double hoursWorked; double hourlyWage;

while (getline(inputFile, line)) { stringstream ss(line); while (getline(ss,data, ',')) { dt[i]=data; i++; }

id = atoi(dt[0].c_str()); name = dt[1]; phone = dt[2]; address = dt[3]; hourlyWage = atof(dt[4].c_str()); hoursWorked = atof(dt[5].c_str()); e.setName(name); e.setPhone(phone); e.setAddress(address); e.setHourlyWaged(hourlyWage); e.setHourlyWorked(hoursWorked);

Employee e(id, name, phone, address, hourlyWage, hoursWorked); e.printCheck(); } return e;

}

void Employee::write(ofstream& outputFile) { outputFile << getEmployeeNumber() << endl << getName() << endl << getPhone() << endl << getAddress() << endl << getHourlyWage() << endl << getHourlyWorked() << endl; return; }

-----------------------------------------------------------------------------------------------

//employee.h

#include #include using namespace std;

class Employee { public: Employee(void); Employee(int, string, string, string, double, double); int getEmployeeNumber(); string getName(); string getAddress(); string getPhone(); double getHourlyWage(); double getHourlyWorked(); void setName(string); void setAddress(string); void setPhone(string); void setHourlyWaged(double); void setHourlyWorked(double); double calcPay(); void printCheck(); Employee read(ifstream&); void write(ofstream&);

private: int employeeNumber; string name; string address; string phone; double hourlyWage; double hoursWorked;

};

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Transactions On Large Scale Data And Knowledge Centered Systems Vi Special Issue On Database And Expert Systems Applications Lncs 7600

Authors: Abdelkader Hameurlain ,Josef Kung ,Roland Wagner ,Stephen W. Liddle ,Klaus-Dieter Schewe ,Xiaofang Zhou

2012th Edition

3642341780, 978-3642341786

More Books

Students also viewed these Databases questions