mardi 22 décembre 2015

do-while infinity loop with if c++

in that loop user should enter an int and program checks if it be odd it will go to next parts, but i cant understand why if user enter an character is not int, program falling in an infinity loop?!

int num;
        do {
            cout << "PLEASE enter the num: ";
            cin >> num;
            if (num % 2 == 0)
                cout << "Number should be odd!" << endl;
            else
                break;
        } while (true);
//...

cause anyway char/int != 0 and if it be ==0 too it should stop at next cin but it wont stop! i tried ws(cin) too but it diden't help me. please tell me "how i can fix that problem", and "Why that happening".

Aucun commentaire:

Enregistrer un commentaire