I have been trying to read in integers from a file do some operations and output them to another file. When I input the integers into an array and then print out the result of come up with random numbers. Is this something to do with ifstream that I don't understand or am I missing something obvious?
#include<iostream>
#include<fstream>
using namespace std;
int main(){
int i=0, test[100];
ifstream reader("in.txt");
while(!reader.eof()){
reader>>test[i];
i++;
}
for(int x=0; test[x]!=-1; x++)
cout<<test[x]<<endl;
return 0;
}
in.txt sample:
1 4 3 2 9 8 18 22 0
2 4 8 10 0
7 5 11 13 1 3 0
-1
the 0 and -1 are sentinels for eol and eof respectively
If there is a much simpler way of doing this I would also like to know that. I am rather new to C++ and am hating the way arrays behave vs other languages.