mercredi 28 septembre 2022

Repeatedly non-stop output when I input char into int variable

The folowing code tells user to input their age, the set to be input interger between 0 and 120, it is capable to deal with wrong input like 'M' or 133 or -1. Warning message goes like this:Warning message

case 1:                                // input age
        cout << "Enter your age: ";
        cin >>  age;
        
        if(age <= 0 || age > 120){     // if the input type or number was wrong, it goes here
            while(1){                
                cout << "Invalid input! Please enter again" << endl << ">>>";
                age = -1;
                cin >> age;
                if(age > 0 && age <= 120) {break;}
            }
        }

However, it'll go wrong if I input something like \ or [. Repeating Warning message

How can I solve this?

Aucun commentaire:

Enregistrer un commentaire