-3

Is there any way to use while loop when user enter wrong number? i.e. while(default) or something. I created goto and it's working but it's lame.

default:
            {
                cin.clear();
                cin.sync();
                cout << "ERROR! Choose other number: ";
                cin >> choose;
                goto again;
            }

1 Answers1

-1

May I suggest:

bool OK;
do
{
    cin >> choose;
    if(OK = !cin.fail())    //if the input is OK
    {
        switch(choose)
        {
        case 1: //do something
            break;
        //case 2: //do something else
        //etc.
        default:
            cin.clear();
            cin.sync();
            cout << "ERROR! Choose other number: ";
            OK = false;
            break;
        }
    }
}
while(!OK);
Michel
  • 338
  • 1
  • 13