Lab 02: SEQUENTIAL ACCESS FILES The toy store in program 2 has hired 3 more part
ID: 3539657 • Letter: L
Question
Lab 02: SEQUENTIAL ACCESS FILES
The toy store in program 2 has hired 3 more part-time workers. Write a
program to add data of the 3 new workers to the file "workers.txt", which
already has data of 5 workers in it. Retain data that are already in "workers.txt".
Do not erase them.
INPUT
EXPECTED OUTPUT
Worker ID: 1006
Hourly rate: 11.5
Number of hours: 18
Worker ID: 1007
Hourly rate: 10.5
Number of hours: 12
Worker ID: 1008
Hourly rate: 10
Number of hours: 15.5
No output on screen, but the following data should be stored in the file "workers.txt":
1001 10.5 20
1002 11 10.5
1003 10 16
1004 10 20
1005 12 15
1006 11.5 18
1007 10.5 12
1008 10 15.5
INPUT
EXPECTED OUTPUT
Worker ID: 1006
Hourly rate: 11.5
Number of hours: 18
Worker ID: 1007
Hourly rate: 10.5
Number of hours: 12
Worker ID: 1008
Hourly rate: 10
Number of hours: 15.5
No output on screen, but the following data should be stored in the file "workers.txt":
1001 10.5 20
1002 11 10.5
1003 10 16
1004 10 20
1005 12 15
1006 11.5 18
1007 10.5 12
1008 10 15.5
Explanation / Answer
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
int main()
{
int n;
cout<<"Enter number of workers data to be added:";
cin>>n;
ofstream out;
out.open("workers.txt",ios::out|ios::app);
int id, hours;
double hourlyRate;
for(int i=0;i<n;i++)
{
cout<<"Worker ID:";
cin>>id;
cout<<"Hourly rate:";
cin>>hourlyRate;
cout<<"Number of hours:";
cin>>hours;
out << id << " " << hourlyRate << " " << hours << endl;
}
out.close();
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.