0

If the file has a list of coordinates and a character:

(1,2,3,A)
// blah blah
(10,20,30,B)
(11,22,33,C)

the expression in the while loop never evaluates to true, Why is this?

string str = "";
char aa, cc, ee, gg, hh, ii;
int bb, dd, ff;
while(cin >> aa >> bb >> cc >> dd >> ee >> ff >> gg >> hh >> ii ||
      cin >> str) {
    if(str != "") {
        cout << str << endl;
    }
    else {
        cout << aa << " " << bb << " " << cc << " " << dd << " " << ee << " " <<  ff << " " << gg << " " << hh << " " << ii << endl;
    }
}

1 Answers1

3

Once you stream has gone into fail-state, i.e., std::ios_base::failbit is set, the stream won't do anything until the stream got cleared. That is, if your first input failed, the second input will fail, too. What would work is spinning things around: first read the string and then using a string stream to see if it decodes into the constituents.

Dietmar Kühl
  • 150,225
  • 13
  • 225
  • 380