for(int sub=0; sub<SIZE; sub++)
{
while (inputFile.eof() == false) //Test for end of file
{
int number = 0;
inputFile >> number; //Read a number
codes[sub] = number;
cout << codes[sub] << endl;
}
}
That while loop simply overwrites codes[0] until it reaches the end-of-file. The other elements in the array are never initialized.
Ahh I see what you are saying, because I do have the for loop, but the while loop doesn't end until the end of the file. How could I go about fixing this? As I need both loops right?
int n;
for(int i = 0; i < SIZE; ++i)
{
if(inputFile >> n)
{
codes[i] = n;
std::cout << codes[i] << std::endl;
}
else
{
// the stream is either in a fail(), bad() or eof() state; deal with error
}
}
Actually, I'd use a std::vector instead of an array, as it's easier to use and much more flexible, but I'm assuming this is an exercise on arrays.