I'm trying to write an array from a file. I'm able to output the numbers that are in the file but when I check to see if the array is populated correctly it instead shows that the whole array is populated by the last number from the file. I'm pretty sure there's something off with my for loops but I'm not sure.
You're going through the whole file, reading each value one at a time into number. At the end of that loop, number will contain the last number. Then in a separate loop, you assign number to every element of array1. You even, strangely, use number as the limit of the loop to fill and display the array.
1 2 3 4 5 6 7 8 9 10 11 12
ifstream inputFile(filename);
if (!inputFile) {
cout << "Cannot open file " << datafile << " Aborting." << endl;
exit(1);
}
int size = 0;
while (inputFile >> number) // >> skips whitespace by default
array1[size++] = number;
for (int i = 0; i < size; i++)
cout << array1[i] << '\n';