Hello,
I'm trying to read some numbers from a file. The last line of the file is a sentinel, -1. I am supposed to write the arrays into another output file later on. I have to use a while loop and the instructions are as follow:
"To input the file items without reading past the end of the file (-1 is the sentinel), use a while loop with two conditions: the empID in a temporary variable that is not negative and the number of employees less than 20. Inside your loop body: Store the "good" employee number, input the number of hours and the payRate in the arrays. The last item in your loop should be the next employee number from the file."
This is my code so far. I cannot figure out how to store "the empID in a temporary variable that is not negative," or use some condition that does not read the -1 sentinel. Any help would be appreciated. Thank you!
int main()
{
string outputFileName;
ofstream outputFile;
string inputFileName;
ifstream inputFile;
constint SIZE = 20;
long empID[SIZE];
double hours[SIZE];
double payRate[SIZE];
double wages[SIZE];
int done = -1;
int count = 0;
while (count < SIZE && empID >= count) //I'm aware the empID >= count part does not work, so please advise me on how to go about this!
{
inputFile >> empID[count];
inputFile >> hours[count];
inputFile >> payRate[count];
count++;
}
Also, my input file is something like this for reference. The max numbers of lines (or employees) before the sentinel is 20.
Well the first thing that would probably help would be to actually try to open your input file for input and your output file for output. But to do that you must first somehow supply the name of the files to open.
Next using a sentinel is not the best way to detect the end of file input. But if that is a requirement then you need another variable to check for this sentinel. Perhaps something like:
Sorry I forgot to include the actual open input lines in the original code, I haven't arranged the lines then yet. Though the instructions asked for two conditions; is there a possible workaround that uses only two conditions instead of three? I haven't learned about stol either, so would you happen to know another substitute? Thanks!
I would recommend just going with the three checks and explain why you used three comparisons in a comment.
But it looks like your instructor is assuming that a read operation never fails, except when encountering eof(), so perhaps you could pre-read the empID before you enter the loop and then read the next ID before the end of the loop, and skip the test of the stream.