Question: C++- Please solve TODO sections and match sample output. This lab practices reading input from file, performing a simple calculation with the input values, and
C++- Please solve TODO sections and match sample output.
This lab practices reading input from file, performing
a simple calculation with the input values, and writing
the results to another file and echoing the output also
to the screen.
Your program will read the following data values from a file,
located in dataFile.txt in the project folder:
H 38 12.35 Haywood JPaymee
S 32000.00 Mickey Mouse
H 40 15.11 John Smith, Jr.
The single character at the beginning of the line designates whether the
employee on that line is an hourly employee (H) or salaried (S).
Line formats:
Hourly: H
Salaried: S
The output to the payroll file (Payroll.txt) will be formatted as follows:
469.30 Haywood JPaymee
615.38 Mickey Mouse
604.40 John Smith, Jr.
The screen output will be formatted as follows:
Name Pay
==============================
Haywood JPaymee 469.30
Mickey Mouse 615.38
John Smith, Jr. 604.40
Several file commands have been provided
- The necessary library for file I/O has been included.
- Variables declared for the input and output streams.
- The file streams opened.
- Displaying the output to the screen.
- Closing the files.
You will be responsible for writing several commands to
read from the input file (inputFile = "dataFile.txt") and
write to the output file (payrollFile = "Payroll.txt")
Consider the following:
- do not prompt for input when reading data from a file
- do not use cin when reading data from a file
*/
#include
#include
#include
#include
using namespace std;
int main()
{
char employeeType; // To hold the employee's type, H for hourly, S for salaried
int hoursWorked; // To hold the number of hours worked for an hourly employee
double hourlyWage; // To hold the hourly wage for an hourly employee
double annualSalary; // To hold the annual salary for a salaried employee
double weeklyPay; // To hold the calculated weekly pay for either type of employee
string fullName; // To hold the employee's full name
char blank; // Used to read a single character
const int WEEKS_PER_YEAR = 52;
// File stream variables
ifstream inputFile; // input file
ofstream payrollFile; // output file
// Open the input file (datafile.txt) and the output file (Payroll.txt)
inputFile.open("datafile.txt");
payrollFile.open("Payroll.txt");
// Display output header to the screen
cout << setw(20) << left << "Name" << setw(10) << right << "Pay" << endl;
cout << "==============================" << endl;
// Loop until the entire input file has been read
while (!inputFile.eof())
{
// TODO: Read the employee type from the input file
// (it will be a single character, H or S)
// Test to see if the employee type is H or S
// We will need to process the pay values differently for each employee type
if (employeeType == 'H') // Hourly employee
{
// TODO: Read the number of hours worked from the input file
// TODO: Read the hourly wage from the input file
// Calculate the hourly employee's weekly pay (hours worked x hourly wage)
weeklyPay = hoursWorked * hourlyWage;
}
else
{ // Salaried employee
// TODO: Read the annual salary from the input file
// Calculate the salaried employee's weekly pay (annual salary / number of weeks in a year)
weeklyPay = annualSalary / WEEKS_PER_YEAR;
}
// Read the full name from the input file
// We can wait until outside the if statement to read the name
// since it is the last item on the line for each type of employee
inputFile.get(blank); // this line required to skip blank space in front of name
// TODO: Read the employee's full name - you will need to use getline
// TODO: Output the employee's weekly pay and full name to the payroll file
// Be sure to include a blank space to separate the pay and the name
// Also, make sure the correct number of decimal places appear in the pay
// Output a single line to the console for the current employee
cout << fixed << setprecision(2) << left << setw(20) << fullName << setw(10) << right << weeklyPay << endl;
}
cout << endl;
// Close the input and output files
inputFile.close();
payrollFile.close();
return 0;
}
// NOTE: There should also be output to a file named Payroll.txt
/* Sample Program Screen Output
Name Pay
==============================
Haywood JPaymee 469.30
Mickey Mouse 615.38
John Smith, Jr. 604.40
Press any key to continue . . .
*/
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
