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:
https://justpaste.it/1dt4e

here is the code that I used:
https://gist.github.com/anonymous/0756cebd8518141d94ae30acd93c9ec6

Now, the problem is the result:
https://imgur.com/a/Dd9rJ

When I add up the individual number of values it shows 1 additional value.
278+261+237+245+280=1301
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
1
2
3
	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.