Basically, you need to use a loop. Note that if the user enters invalid input, cin >> sel fails and cin enters an error state. So you can test if the read failed, and if so, you clear the error state, discard what is left on the input stream, (optionally) print an error message, and then print the menu and get the user's selection again.
An example of this would be:
Code:
int sel;
for (;;)
{
cout << "1: Do This" << endl;
cout << "2: Do this" << endl;
cout << "Selection? ";
if (!(cin >> sel))
{
cin.clear();
cin.ignore(numeric_limits<std::streamsize>::max(), '\n');
cout << "Invalid selection. Please try again.\n";
}
else
{
cin.ignore(numeric_limits<std::streamsize>::max(), '\n');
break;
}
}
You could also use a do while loop, perhaps with a function.
EDIT:
Incidentally, it occurred to me that my original example does not take into account valid followed by invalid input on the same line. You have a choice in such situations: either read the entire line as a string, parse it, and then report an error if invalid input is found at all, or simply accept the valid portion, then discard the invalid portion. This is what I have done with my corrected example with another cin.ignore() call before the break.