// This program reads data from a file into an array. // In this case it is attempting to read in 10 numbers but the file contains only nine numbers! #include #include using namespace std; int main() { const int ARRAY_SIZE = 10; // Array size int numbers[ARRAY_SIZE]; // Array with 10 elements int count = 0; // Loop counter variable int count2; //output loop counter variable ifstream inputFile; // Input file stream object // Open the file. inputFile.open("NineNumbers.txt"); // Read the numbers from the file into the array. while (count < ARRAY_SIZE && inputFile >> numbers[count]) count++; //count tracks how many are read // Close the file. inputFile.close(); // Display the numbers read: //this loop had to be modified in order to not display garbage at the end of the array //when the file contained less than ten numbers. cout << "The numbers are: "; for (count2 = 0; count2 < count; count2++) cout << numbers[count2] << " "; cout << endl; return 0; }