The program output Should be:
The numbers are: 101 102 103 104 105 106 107 108 108 110
But my output is:
The numbers are: 0 0 0 0 0 0 0 0 1606416272 32767
This is my code:
// This program reads data from a file into an array.
#include <iostream>
#include <fstream> // To use ifstream
using namespace std;
int main()
{
const int ARRAY_SIZE = 10; // Array size
int numbers[ARRAY_SIZE]; // Array number with 10 elements
int count = 0; // Loop counter variable
ifstream inputFile; // Input file stream object
// Open the file.
inputFile.open("TenNumbers.rtf");
// Read the numbers from the file into the array.
while (count < ARRAY_SIZE && inputFile >> numbers[count]){
count++;
}
// Close the file.
inputFile.close();
// Display the numbers read:
cout << "The numbers are: ";
for (count = 0; count < ARRAY_SIZE; count++){
cout << numbers[count] << " ";
}
cout << endl;
return 0;
}
This is the contents of the TenNumbers.rtf file I'm reading the data from:
101
102
103
104
105
106
107
108
109
110
UPDATE 1: I tried using txt file but the results are similar.
The numbers are: 0 0 0 0 0 0 0 0 1573448712 32767
UPDATE 2:
I found where the issue was. After running if (inputFile.good())
I found out the file was not getting opened.