"Your program should use the following one-dimensional arrays.
empId: An array of long integers to hold employee identification numbers. Assume there will be no more than 20 identification numbers, but your code should check that the file doesn't enter anymore. If an attempt is made to enter 21, process only the first 20.
hours: An array of doubles to hold the number of hours worked by each employee. Fractional hours are possible.
payRate: an array of doublesnto hold each employee's hourly pay rate.
wages: an array to hold each employee's gross wages."
There is no harm in self-help especially when the tutorials on this site are so easily available. The chances are you will learn a lot more.
Use this as a start -> http://www.cplusplus.com/doc/tutorial/files/ There is a sample program on using a while loop and getline to read a text file which can be adapted to what you want. (You won't need getline though)
Also, since your file is limited by the value -1, you might have to think carefully how the while condition might be modified. This is necessary to allow for less than 20 lines of data.
You might end up with something along the lines of what jonnin has written, ie read the first value on each line and stop reading if the vale is -1 and if it isn't keep reading the other values on the line, also provided you are still within the 20 limit.
(Hint: if you can, delete the line with -1 on it and let the machine (stream) work out the end of data via the while statement I have written.)
That's OK, I thought that might be the case. So all you have to do is read the first value in and adjust the while condition and loop along the following lines:
#include <iostream>
#include <string>
#include <fstream>
usingnamespace std;
constint SIZE=20;
int main()
{ int empId[SIZE];
double hours[SIZE];
double payRate[SIZE];
double wages[SIZE];
string inFileName;
int count = 0;
cout<<"Infile is "<<endl;
cin>>inFileName;
ifstream inFile(inFileName.c_str());
if (!inFile.is_open())
{ cout<<"Error opening file."<<endl;
return 1; // No point in continuing
}
while(count<SIZE && inFile>> empId[count] && empId[count] != -1)
{ inFile >> hours[count] >> payRate[count];
wages[count] = hours[count]*payRate[count];
count++;
}
for (int i=0; i<count; i++)
{ cout << empId[i] << " " << wages[i] << endl;
}
system ("pause");
return 0;
}
PLEASE ALWAYS USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post. http://www.cplusplus.com/articles/jEywvCM9/
Hint: You can edit your post, highlight your code and press the <> formatting button.