Read input file into array using while loop?

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!


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23

int main()
{
	string outputFileName;
	ofstream outputFile;
	string inputFileName;
	ifstream inputFile;

	const int 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.
1
2
3
4
5
6
7
8
9
 
7846164 37 100.5
6528446 20 7.5
1254789 35 12.5
2478591 10 250
9634587 4 700
3326459 40 10
-1
Last edited on
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:

1
2
3
4
5
6
7
8
        string temp;
	while (count < SIZE && inputFile >> temp && temp != "-1") 
	{
		empID[count] = stol(temp);
		inputFile >> hours[count];
		inputFile >> payRate[count];
		count++;
	}
Last edited on
Thank you jlb, the code worked!

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 haven't learned about stol

Change the type of temp to a long.

1
2
3
4
5
6
7
        long temp;
	while (count < SIZE && inputFile >> temp && temp != -1)   // note - no quotes on -1
	{      empID[count] = temp;
		inputFile >> hours[count];
		inputFile >> payRate[count];
		count++;
	}

I see, thank you so much AbstractionAnon!

My only issue now is how to make the while loop execute with only two conditions. I would greatly appreciate any chiming in!
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.
1
2
3
4
5
6
7
8
9
long temp;
inputFile >> temp;
while(count < SIZE && temp != -1)
{
   empID[count] = temp;
   inputFile >> hours[count >> payRate[count];
   count++;
   inputFile >> temp;
}
It worked, thank you lots jlb!!
Topic archived. No new replies allowed.