Working with .txt files on C++

Hello, we were given an assignment where we work with a given .txt file.

Instructions for the Assignment and .txt file are located here:

here is the code that I used:

Now, the problem is the result:

When I add up the individual number of values it shows 1 additional value.
Total values=1300

edit: I forgot that my professor told us that "In a lot of Windows computers, the last value is counted twice."

hence: the n--; at line 46
I tried doing n_5--; after line 46 and it worked but I'm not sure if this is the proper way to do this.

Last edited on
	while (!fin.eof())
		fin >> temp;
don't loop on eof, loop on the reading operation while( fin>>temp ){
that way you won't have the "last value counted twice" issue.

Suppose that your file ends with a line break 42\n
after consuming the last number you'll have \n, so you didn't reach the end-of-file. You attempt to read one more time and fail.
Notice where you check for eof(), that last attemp (that failed) will be counted as valid.

Now suppose that the file doesn't end with a line break 42, then after consuming the last number you do reach the end-of-file, and will have the correct count.
┬┐how could you decide whether you need to do --n or not?

you wouldn't have this issue if you check the reading operation.
Topic archived. No new replies allowed.