Hi, I'm trying to read some numbers from a file called numbers.txt. There are 10 numbers in this file in this kind of format:
numbers.txt
1 2 3 4 5 6 7 8 9 10
87
89
91
93
95
97
99
101
103
105
Once I read these numbers from the file I want to store them in an array of size 10. However, when I do a cout statement to see the contents of my array I'm noticing the first element is not being read and the last element is being repeated twice. I really don't know why that is the case.
Replace the whole of your while loop and accompanying lines (i.e lines 16-24) with the single line for (int i = 0; i < numbersFile.size(); i++) intFile >> numbersFile[i];
I was just wondering what exactly was the logical error I made in order to get the incorrect output in the first place?
The first item in your file was read with the statement while (intFile >> tmpInt)
but you didn't do anything with this input.
You then tried to read 10 more items, but there were only 9 more left in the file, so the last read would have failed. You would then end up assigning the last correctly read item in tmpInt for a second time to the last element of numbersFile[].