Lab 02: SEQUENTIAL ACCESS FILES A toy store has 5 part-time workers who are paid
ID: 3539656 • Letter: L
Question
Lab 02: SEQUENTIAL ACCESS FILES
A toy store has 5 part-time workers who are paid by hours. Each worker has a
worker ID number. Write a program that asks for the worker ID number, the
hourly rate and the number of work hours per week of each worker. The
program should store the data in a sequential access file named "workers.txt".
Erase existing data in "workers.txt" if the file is not empty.
INPUT
EXPECTED OUTPUT
Worker ID: 1001
Hourly rate: 10.5
Number of hours: 20
Worker ID: 1002
Hourly rate: 11
Number of hours: 10.5
Worker ID: 1003
Hourly rate: 10
Number of hours: 16
Worker ID: 1004
Hourly rate: 10
Number of hours: 20
Worker ID: 1005
Hourly rate: 12
Number of hours: 15
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
INPUT
EXPECTED OUTPUT
Worker ID: 1001
Hourly rate: 10.5
Number of hours: 20
Worker ID: 1002
Hourly rate: 11
Number of hours: 10.5
Worker ID: 1003
Hourly rate: 10
Number of hours: 16
Worker ID: 1004
Hourly rate: 10
Number of hours: 20
Worker ID: 1005
Hourly rate: 12
Number of hours: 15
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
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);
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.