The objective of this Exercise is to demonstrate the use of a file for data inpu
ID: 3888618 • Letter: T
Question
The objective of this Exercise is to demonstrate the use of a file for data input. Write a program for a payroll calculation: given a name, hours worked, and an hourly wage, calculate and print the total amount earned in a sentence. Your output might look something like the following: arriette Roadman xercise 4 an 23. 2016 ata is being read fron file exAdata.txt ile contains: nane (single word), hours worked float), Wage (float. in dollars per hour) amantha uorked 24.50 hours at $?.85 per hour and earned $192.32 for the ueek ress any key to continue.. . This data file was used for input to the program that produced the output shown above: ex4data-Notepad Eile Help File contains name (single word), hours worked (loat), wage (float, in dollars per hour) Samantha 24.5 .85 to note Please review the documents File Creation and File Input & Output. You will need to create a data file to use for input. You will choose the name of the input file. You might use something like "ex4data txt" For this Exercise you should set up the input file to contain at least 4 lines of data: First line:information on how the file is set up Second line: a name (either one word, or several words, but must be character data) Third line: a value for hours worked, should be a decimal number Fourth line: a value for hourly wage, no S included, should be a decimal number Write your code to read the values from the input file, including the first line, calculate the wage, and print the information requested. Your output doesn't need to be identical to what is shown, but it does need to have the ame content The input values are echoed as part of the wage sentence. Be sure to: use#include print the file name include a S sign in the cout statement for the hourly wage as well as for the total earnings. use coutExplanation / Answer
#include <cstdlib>
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
using namespace std;
// Prototype functions
double calwag ();
void getinfo();
voidprintwag();
void printtab();
void rept();
void readinfo();
// Defining terms for the program.
int id,result;
double payRate, workinghours, workwage;
ofstream outputFile;
ifstream inputFile;
string file,strings;
void main()
{
cout << "Please enter the name of the file you wish to save to: ";
cin >> file;
outputFile.open(file.c_str());
// Get the information of the employee
getinfo();
// Calculate the total wage of the employee
workwage = calwag();
printtab();
printwag();
rept();
readinfo ();
}
// Function for taking input
void getinfo()
{
cout << " enter the employee id, payrate, and hours worked: " << endl;
cin >> id >> payRate >> workinghours;
//Loop
while ((id < 0) || payRate < 0 || workinghours < 0)
{
cout << " enter values which are valid please (Positive numbers): " << endl;
cin >> id >> payRate >> workinghours;
}
}
// calculating the Wage
double calwag()
{
double wages;
return wages = payRate * workinghours;
}
// print information to the outputFile
voidprintwag()
{
outputFile << setw(10) << id
<< setprecision(2) << fixed << setw (10)
<< payRate
<< setprecision(2) << fixed << setw (10)
<< workinghours
<< setprecision(2) << fixed << setw (10)
<< workwage << endl;
}
// printing the table to the outputFile
void printtab()
{
outputFile << setw(5) << "ID" << setw (5) << "Rate" << setw(5) << "Hours"
<< setw(5) << "Wage"<<endl;
}
// asking the user if the process should be repeated
void rept()
{
char answer;
cout << "Would you like to continue (y/n): ";
cin >> answer;
if (answer == 'y'|| answer == 'Y')
{
getinfo();
workwage = calwag();
printwag();
rept();
}
else
{
outputFile.close();
cout << file << " was closed " << endl;
}
}
void readinfo()
{
char repans;
cout << "Would you like to readinfo into the file? (y/n): ";
cin >> reptrAnswer;
if (reptrAnswer == 'y' || reptrAnswer == 'Y')
{
inputFile.open(file.c_str());
while (!inputFile.eof())
{
getline(inputFile, strings);
cout << strings<< endl;
}
cout << "data is being read from file " << file << endl;
}
inputFile.close();
exit(0);
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.