vendredi 22 novembre 2019

How to fix a do/while statement if I put a char.C++

So my problem is that I want to stop every input except 1 and 2 so I do do/while statement and it works for integers,but If a put a letter (a,b,q) the do/while statement just broke.It start spamming the output of the do/while statement and the program broke.How to fix that.

void main() {
    int input1;
    do {
        cin >> input1;
        if (!cin) {

            cout << "NO" << endl;
            main();
        }
    } while (input1 < 1 || input1>2);

    switch (input1) {
    case 1:cout << 1;
        break;
    case 2:cout << 2;
        break;
    }
}```

Aucun commentaire:

Enregistrer un commentaire