Hello, I was writing this code where it basically reads integer values from a text file in one function, and then takes another function to sort out how many even numbers there are. Only problem is, it's not taking in any values. I even checked: I put all 3s in my txt file, and it's still saying that all ten values are even. Any suggestions on how to fix this?
Perhaps the file did not open or a read did fail. Those can be tested:
1 2 3 4 5 6 7 8 9 10 11 12
int readfile(int Array[], int N)
{
ifstream Main;
Main.open("Data.txt");
if ( ! Main ) return 0; // file did not open
int i = 0;
while ( i < N && Main >> Array[i])
{
++i;
}
return i;
}
You can also print the values that you did read to see what you did read:
1 2 3 4 5 6 7
int Array[10], counter = 0;
int numbers = readfile(Array, 10);
for (int i = 0; i < numbers; i++)
{
cout << i << ": " << Array[i] << '\n';
}
You will remove the print loop, when that part works correctly.