I am trying to read double values from a text file and store them in a dynamic array of doubles. I have 4 double values in my file, each value in newline. I am using while loop to iterate through all the elements of m_numbers and storing a double value in them. while statement should fail after 4th iterations but it is doing 5 iterations. And that is resulting into wrong return value of the function. Also it seems like only first value is stored in all the elements.
double* m_numbers = nullptr;
int m_numCount = 4;
these variables are already initialized in header file.
file content:
6
12.2
3
34.1
bool Numbers::load()
{
int i = 0;
if (m_numCount > 0) {
delete[] m_numbers;
m_numbers = new double[m_numCount+1];
ifstream fin(m_filename);
while (!fin.fail()) {
fin >> m_numbers[i];
i++;
}
}
return i == m_numCount;
}