Add the following to the code below Create the sample.txt file as in the Word do
ID: 3536698 • Letter: A
Question
Add the following to the code below
Create the sample.txt file as in the Word document using notepad and save the file as sample.txt. Be sure to follow the instructions.
You will add one value-returning function to this source code. In the Word document, I have included void functions and reference parameters. Add a function called calculateSalary that will return a double and takes in two arguments %u2013 pHours and pWages (value parameters; make sure the data types match as in source code). Include the function prototype for this function. In the code, replace hours * wages with calculateSalary and pass the parameters necessary. Do not declare a variable called salary.
Source code
/*
Name: sample.cpp
Author: VG
Date: 5/12/2012
Description: This program reads data from a file and then writes to an output file.
*/
//header files
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
using namespace std;
//Function prototypes
void message();
void printTitle(ofstream& outp);
void fileOpen(ifstream& inp, ofstream& outp);
void fileClose(ifstream& inp, ofstream& outp);
void readData(ifstream& inp, string& pName, int& pAge, string& pMF,
int& pHours, double& pWages);
int main()
{
//declare file stream variables
ifstream inFile;
ofstream outFile;
//declare variables
string name, M_F;
int hours, age;
double wages;
fileOpen(inFile, outFile); //call file open function to open the input and output file.
if (inFile)
message(); //message sent to monitor
else //message if input file is not found
{
cout << "Cannot open file." << endl;
system("Pause");
return 1;
} //end if
//Setting the fixed format and two decimal places
outFile << fixed << showpoint << setprecision(2);
//call readData function to read the first line in input file
readData(inFile, name, age, M_F, hours, wages);
printTitle(outFile); //call printTitle function and print the headings in output file
//write the first processed information to output file.
outFile << left << setw(19) << name << setw(5) << age << setw(7) << M_F
<< setw(7) << right << hours << setw(7) << wages << setw(9)
<< hours * wages;
while (!inFile.eof())
{
readData(inFile, name, age, M_F, hours, wages); //read all the data in input file and process the information
outFile << left << setw(20) << name << setw(5)<< age << setw(7) << M_F
<< setw(7) << right << hours << setw(7) << wages << setw(9)
<< hours * wages; //write the test of the processed information to output file.
}//end while
outFile << endl;
fileClose(inFile, outFile); //call the file close function to close the input and output file.
system("Pause");
return 0;
} //end main
//Function Prints a message to the monitor explaining what the program does and what the name //of the output file is.
void message()
{
cout << "This program reads data from input file and calculates the wages "
<< "then writes it to an output file. The name, age, male/female, "
<< "hours worked and wages are in the input file. In addition to "
<< "writing this information, the salary is also written in the file. "
<< "Check the sample.out file for the report. " << endl;
}//end message
//Function prints the title in the output file
void printTitle(ofstream& outp)
{
outp << left << setw(20) << "Name" << setw(5)<<"Age" << setw(7) << "Male/"
<< setw(7) << "Hours" <<setw(7) << "Wages" << setw(9) << "Salary" << endl;
outp << right << setw(30) << "Female" << endl;
outp << "------------------------------------------------"
<< "----------------" << endl;
} // end printTitle
//Function to open the input file and output file
void fileOpen(ifstream& inp, ofstream& outp)
{
inp.open("sample.txt");
outp.open("sample.out");
}// end fileOpen
//Function to close the input file and output file
void fileClose(ifstream& inp, ofstream& outp)
{
inp.close();
outp.close();
} //end fileClose
//Function to read data from input file. Notice that the delimiting character for the string is #.
void readData(ifstream& inp, string& pName, int& pAge, string& pMF, int& pHours, double& pWages)
{
getline(inp, pName,'#'); This can read a string with a space. (First name space last name is read here)
inp >> pAge;
getline(inp, pMF,'#');
inp >> pHours;
inp >> pWages;
} // end getData
Explanation / Answer
//header files
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
using namespace std;
//Function prototypes
void message();
void printTitle(ofstream& outp);
void fileOpen(ifstream& inp, ofstream& outp);
void fileClose(ifstream& inp, ofstream& outp);
void readData(ifstream& inp, string& pName, int& pAge, string& pMF,
int& pHours, double& pWages);
double calculateSalary(int pHours, double pWages);
int main()
{
//declare file stream variables
ifstream inFile;
ofstream outFile;
//declare variables
string name, M_F;
int hours, age;
double wages;
fileOpen(inFile, outFile); //call file open function to open the input and output file.
if (inFile)
message(); //message sent to monitor
else //message if input file is not found
{
cout << "Cannot open file." << endl;
system("Pause");
return 1;
} //end if
//Setting the fixed format and two decimal places
outFile << fixed << showpoint << setprecision(2);
//call readData function to read the first line in input file
readData(inFile, name, age, M_F, hours, wages);
printTitle(outFile); //call printTitle function and print the headings in output file
//write the first processed information to output file.
outFile << left << setw(19) << name << setw(5) << age << setw(7) << M_F
<< setw(7) << right << hours << setw(7) << wages << setw(9)
<< calculateSalary(hours, wages);
while (!inFile.eof())
{
readData(inFile, name, age, M_F, hours, wages); //read all the data in input file and process the information
outFile << left << setw(20) << name << setw(5)<< age << setw(7) << M_F
<< setw(7) << right << hours << setw(7) << wages << setw(9)
<<calculateSalary(hours, wages); //write the test of the processed information to output file.
}//end while
outFile << endl;
fileClose(inFile, outFile); //call the file close function to close the input and output file.
system("Pause");
return 0;
} //end main
//Function Prints a message to the monitor explaining what the program does and what the name //of the output file is.
void message()
{
cout << "This program reads data from input file and calculates the wages "
<< "then writes it to an output file. The name, age, male/female, "
<< "hours worked and wages are in the input file. In addition to "
<< "writing this information, the salary is also written in the file. "
<< "Check the sample.out file for the report. " << endl;
}//end message
//Function prints the title in the output file
void printTitle(ofstream& outp)
{
outp << left << setw(20) << "Name" << setw(5)<<"Age" << setw(7) << "Male/"
<< setw(7) << "Hours" <<setw(7) << "Wages" << setw(9) << "Salary" << endl;
outp << right << setw(30) << "Female" << endl;
outp << "------------------------------------------------"
<< "----------------" << endl;
} // end printTitle
//Function to open the input file and output file
void fileOpen(ifstream& inp, ofstream& outp)
{
inp.open("sample.txt");
outp.open("sample.out");
}// end fileOpen
//Function to close the input file and output file
void fileClose(ifstream& inp, ofstream& outp)
{
inp.close();
outp.close();
} //end fileClose
//Function to read data from input file. Notice that the delimiting character for the string is #.
void readData(ifstream& inp, string& pName, int& pAge, string& pMF, int& pHours, double& pWages)
{
getline(inp, pName,'#'); This can read a string with a space. (First name space last name is read here)
inp >> pAge;
getline(inp, pMF,'#');
inp >> pHours;
inp >> pWages;
} // end getData
// function to calculate salary...
double calculateSalary(int pHours, double pWages);
{
return static_cast<double> (pHours)*(pWages);
} // end of salary method.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.