int menu and char's being input

I am writing a code where I ask the user to input a value of 1 2 or 3. If the user enter's a char, the code breaks and enters an infinite loop.

How do I stop this?

I am using a function to return the value of their input, and I am using the reference & to change their input.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
 int menu(int &x)
{
	cout<<"*******************Paper, Rock, Scissor*******************\n";
	cout<<"\nChoose from the following menu:\n1 for Rock -- 2 For Paper -- 3 For Scissors\nYour Entry: ";
	cin>> x;
	int count = 0;
	while (x != 1 || x != 2 || x != 3){
		cout<<"please enter a valid x value! Enter 1, 2, or 3: ";
		break;}
		cin >> x;
	while (x < 1 || x > 3 && count < 2){
		cout<<"\nPlease enter a 1 for Rock, 2 for Paper, 3 for Scissors: ";
		cin>> x;	
		count++;
		if (count > 3){
			cout<<"You have run out of attempts to enter a valid input!";
			break;}
	}

	return x;
}  
1
2
3
4
5
char x;
int y;
cin>>x;
if(isdigit(x))y=atoi(x);
//you can do something with y here 


I don't think its a prper way to do this but that's the way I do it
Topic archived. No new replies allowed.