while (myfile >> line)
{
if (myfile.fail())
{
cout << "Error, line is not a number." << endl;
j = j + 1;
}
mydata[i] = line;
i = i + 1;
}
Your problem is that the first line while (myfile >> line)
will evaluate to false
when it gets to the line gh
, so your conditional gets skipped altogether. If you are looking to count errors, your code would need to be more like
std::string sLine;
while (std::getline(myfile, sLine))
{
std::istringstream iss(sLine);
if (!(iss >> line))
{
cout << "Error, line is not a number." << endl;
++j;
}
else
{
mydata[i] = line;
++i;
}
}