Please us code tags when posting code. Highlight the code and click the <> button to the right of the edit window. I've indented the code and put it in tags below.
Change the file name at line 27 so you don't overwrite your input file.
Each time line 19 reads a new line it throws away the previous results. That's probably not what you want.
Lines 31-36 read worker data from cin, which is very similar to what's going on at line 19. Should the program prompt the user to enter the data or read it from a file?
If you're reading the worker data from an input file then I suggest you structure the code like this:
- open the input file, complain if that fails
- open the output file, complain if that fails
- while (workData >> workerId >> hourlyRate >> workHours) {
compute the gross pay for this worker
write the output for this worker to the output file.
- close the input and output files
If you do it this way then it will work for any number of input records.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
|
#include <cstdlib>
#include <iostream>
#include <fstream>
using namespace std;
int
main()
{
double workerId = 0;
double hourlyRate = 0.0;
double workHours = 0.0;
ifstream workData;
workData.open("workers.txt", ios :: in);
if (workData.is_open()) {
for (int count = 0; count < 5; count++) {
workData >> workerId >> hourlyRate >> workHours;
}
workData.close();
} else {
cout << "The file could not be opened." << endl;
}
ofstream update;
update.open("workers.txt", ios :: out | ios :: app);
if (update.is_open()) {
for (int i = 0; i < 3; i++) {
cout << "Enter ID Number: ";
cin >> workerId;
cout << "Enter rate: ";
cin >> hourlyRate;
cout << "Enter hours: ";
cin >> workHours;
update << workerId << " " << hourlyRate << " " << workHours << " " << endl;
}
workData.close();
} else {
cout << "The file could not be changed." << endl;
}
//system("pause");
return 0;
}
|