Create worker.txt in the format shown below. Save the text file in a folder. Thi
ID: 3685654 • Letter: C
Question
Create worker.txt in the format shown below. Save the text file in a folder. This file contains four workers each with three pieces of information that are divided into columns separated by a space. Column 1 is the name of the worker, column 2 is the hours worked, and column 3 represents an hourly wage. Your program is to do the following: Write a piece of code to open the worker.txt file and store the information into input file stream. Write a piece of code which computes the earnings and output it to a file with the format shown in illustration 2. Also print the earnings to the console.Explanation / Answer
worker.txt
williams 8 9
tony 11 10
ronie 12 9
WorkerEarnings.cpp
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
int h,p;
string name;
string worker[3];
int earn[3];
int i=0,j=0;
ifstream myfile;
//Opened the file
myfile.open("D:\xyz\worker.txt");
//Read the contents from the file and stored in array
while(myfile>>name>>h>>p)
{
worker[i]=name;
earn[i]=h*p;
//calculated earnings
cout<<name <<" earned amount "<<h*p<<" ";
i++;
}
//close the file
myfile.close();
ofstream myfile1;
//Opened the file to write into it
myfile1.open ("D:\xyz\earnings.txt");
//Write the output into the file
for(j=0;j<3;j++)
{
myfile1 <<worker[j] <<" "<<earn[j]<<" ";
}
//close the file
myfile1.close();
}
______________________________________________________________________________________
output:
D:xyzearnings.txt (Here xyz is the folder in D drive.)
williams 72
tony 110
ronie 108
____________________________________________________________________________________________
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.