catch input errors

My very simple program is to register hotel guests. At the beginning, the user should input 1, 2, 3 or 4 to go to the next step.

What is the best way to catch an input which is not in '1,2,3,4' then report an error to the user and ask for a new input.

This is a fundamental problem in programming I think. The user inputs an unexpected input, maybe just presses 'enter'. How do we look at the input, compare it with what is exepected, and if it is not in the set of expected values, go back to the beginning?

How is that handled?

Thanks!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
using namespace std;

int main(){
	int input = 0;
	cout << "Enter a number(1,2,3,4): " << endl;
	cin >> input;

	if( (input!=1) && (input!=2) && (input!=3) && (input!=4) ){
		cout << "You entered a wrong number" << endl;
	}

	system("pause"); 
}


If the user just presses enter, then nothing will happen. He won't be able to continue until he enters something.

I hope this helps.
Topic archived. No new replies allowed.