I have this code to read doubles from a file:
int main() {
ofstream out("output.txt");
out << "3.0x1.5y-2.0z0.5" << endl;
out.close();
ifstream in("output.txt");
while (true) {
double x;
in >> x;
if (in.eof())
break;
if (x) { // got a double
cout << x << endl;
} else { // skip invalid char
in.clear();
in.ignore(1);
}
}
return 0;
}
The problem is that x
is taking unexpected values. Why does it read 5, -2 and 0.5 for file with content “3.0×1.5y-2.0z0.5” instead of 3.0, 1.5, -2 and 0.5?
5