jeudi 10 novembre 2016

while loop with >> exiting incorrectly

I'm trying to use a while loop that reads an ifstream object from a txt file, then checks some if else statements based on the input. It's supposed to read the txt file line by line. First it reads a char, then some ints, then moves on to the next line in the file. It looks like this:

ifstream ins;
char shape = '?';

while (ins >> shape) {
        if (shape == 'L') {
            // do something
        }
        else if (shape == 'C') {
            // do something
        }
        else if (shape == 'T') {
            // do something
        }
        else if (shape == 'R') {
            // do something
        }
        else {
            // do something
        }
    }
    ins.close();

For some reason, if shape = 'R', it doesn't move on to the next line of the file and read the next shape char. Instead, it (ins >> shape) fails and the it exits the loop. It works perfectly fine with the rest of the conditions. Only fails with 'R'.

I've tried using ins.clear() and ins.ignore(numeric_limits::max()); but it doesn't make any difference. Let me know if you need more info, thanks.

Aucun commentaire:

Enregistrer un commentaire