Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

in c++ i am also having trouble understanding how to get the program to read the

ID: 3805964 • Letter: I

Question

in c++ i am also having trouble understanding how to get the program to read the file

Question 1:

Let’s consider a text file named “employees.txt”. The file contains data organized according to the following format:

            John     Smith      10     15

     Sarah     Johnson   40    12

     Mary      Taylor    27     13

     Jim      Stewart   25    8

For instance, “John” is the first name, “Smith” is the last name, “20” is the number of hours per week, and “9” is the hourly rate.

Write a program that computes the weekly salary of each employee. The program prints the first name, last name, and weekly salary of each employee in a file names “results.txt”. You should use a function to read/process the data from the file, this function must have parameters, like in our example.

Explanation / Answer

#include<iostream>
#include<fstream>

using namespace std;
void readData(ifstream &inputFile, ofstream &outputFile)
{
string firstName, lastName;
int hours, rate;
if (inputFile.is_open()) {
while (!inputFile.eof()) {
inputFile>>firstName>>lastName>>hours>>rate;
outputFile<<firstName <<" "<<lastName<<" "<<(hours * rate)<<endl;
}
cout<<"File has been generated"<<endl;
}
}
int main() {

ifstream inputFile;
inputFile.open("employees.txt");
ofstream outputFile;
outputFile.open ("result.txt");
readData(inputFile, outputFile);
inputFile.close();
outputFile.close();
return 0;
}

Output:

result.txt

John   Smith   150
Sarah   Johnson   480
Mary   Taylor   351
Jim   Stewart   200