Input Validation

I'm writing a program for a game of Rock, Paper, Scissors.
I've written it and it works and now I'm doing the extra credit for it which is
Adding validating input from the user (if the user enters a "K" or "k", you would ask the user again until he/she enters only P/p/s/S/R/s)
I know it might seem simple but I've been trying different things and haven't been able to make it work without actually messing up the program so help please?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include <iostream>
#include <string>
using namespace std;
int main(){
	char player1, player2;
	string check;
	do
	{

		cout << "Please make your choice Player One [P/R/S]" << endl;
		cin >> player1;

			cout << "Please make your choice Player Two [P/R/S]" << endl;
		cin >> player2;
		
		if (player1 == player2)
			cout << "Nobody wins" << endl;
		if ((player1 == 'P' && player2 == 'p') || (player1 == 'p' && player2 == 'P'))
			cout << "Nobody wins" << endl;
		if ((player1 == 'S' && player2 == 's') || (player1 == 's' && player2 == 'S'))
			cout << "Nobody wins" << endl;
		if ((player1 == 'R' && player2 == 'r') || (player1 == 'r' && player2 == 'R'))
			cout << "Nobody wins" << endl;

		if ((player1 == 'P' || player1 == 'p') && (player2 == 'R' || player2 == 'r'))
			cout << "Paper covers rock, Player 1 wins" << endl;
		if ((player1 == 'R' || player1 == 'r') && (player2 == 'P' || player2 == 'p'))
			cout << "Paper covers rock, Player 2 wins" << endl;

		if ((player1 == 'P' || player1 == 'p') && (player2 == 'S' || player2 == 's'))
			cout << "Scissors cut paper, Player 2 wins" << endl;
		if ((player1 == 'S' || player1 == 's') && (player2 == 'P' || player2 == 'p'))
			cout << "Scissors cut paper, Player 1 wins" << endl;

		if ((player1 == 'R' || player1 == 'r') && (player2 == 'S' || player2 == 's'))
			cout << "Rock breaks scissors, Player 1 wins" << endl;
		if ((player1 == 'S' || player1 == 's') && (player2 == 'R' || player2 == 'r'))
			cout << "Rock breaks scissors, Player 2 wins" << endl;

		cout << "Do you want to continue? [y/n]" << endl;
		cin >> check;
	} while (check != "n");
	system("pause");
	return 0;
}
On line 39 put:
else continue;
This will bring the control back to the top of the loop to have the user pick again.

Good luck!
Last edited on
When I do that it does make the user pick again but if the user picks anything else like a or b it just says nobody wins.

I need to make it were if anything but P,p,S,s,R,r is inputted will say something like "choice is invalid please put [P/S/R]"
Then put on line 39 instead:
1
2
3
4
5
else
{
cout << "Invalid input. Try again." << endl;
continue;
}
Okay so when I put in two wrong inputs like g and y it works it says invalid input try again but if I put in the right inputs like r and p it gives the results of who wins but right after it still says invalid input

and putting the continue; never allows it to be asked "Do you want to continue?"
Please post the entire code you have so far so I can look at it.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include <iostream>
#include <string>
using namespace std;
int main(){
	char player1, player2;
	string check;
	do
	{

		cout << "Please make your choice Player One [P/R/S]" << endl;
		cin >> player1;

		cout << "Please make your choice Player Two [P/R/S]" << endl;
		cin >> player2;
		
		if (player1 == player2)
			cout << "Nobody wins" << endl;
		if ((player1 == 'P' && player2 == 'p') || (player1 == 'p' && player2 == 'P'))
			cout << "Nobody wins" << endl;
		if ((player1 == 'S' && player2 == 's') || (player1 == 's' && player2 == 'S'))
			cout << "Nobody wins" << endl;
		if ((player1 == 'R' && player2 == 'r') || (player1 == 'r' && player2 == 'R'))
			cout << "Nobody wins" << endl;

		if ((player1 == 'P' || player1 == 'p') && (player2 == 'R' || player2 == 'r'))
			cout << "Paper covers rock, Player 1 wins" << endl;
		if ((player1 == 'R' || player1 == 'r') && (player2 == 'P' || player2 == 'p'))
			cout << "Paper covers rock, Player 2 wins" << endl;
		
		if ((player1 == 'P' || player1 == 'p') && (player2 == 'S' || player2 == 's'))
			cout << "Scissors cut paper, Player 2 wins" << endl;
		if ((player1 == 'S' || player1 == 's') && (player2 == 'P' || player2 == 'p'))
			cout << "Scissors cut paper, Player 1 wins" << endl;

		if ((player1 == 'R' || player1 == 'r') && (player2 == 'S' || player2 == 's'))
			cout << "Rock breaks scissors, Player 1 wins" << endl;
		if ((player1 == 'S' || player1 == 's') && (player2 == 'R' || player2 == 'r'))
			cout << "Rock breaks scissors, Player 2 wins" << endl;
		else
		{
			cout << "Invalid input. Try again." << endl;
			continue;
		}
		cout << "Do you want to continue? [y/n]" << endl;
		cin >> check;
	} while (check != "n");
	system("pause");
	return 0;
}
It seems to run perfectly for me. If the user enters an invalid input, it's only correct to say that nobody wins. The program immediately asks for more input. This is a neat and professional way to handle such a situation.
The only reason this code would be unsatisfactory is if it was part of an assignment and you had to give a specific output.

I think it's good--what do you think?
Did you try putting the valid inputs? When player 1 enters p and player 2 enters r the result is paper covers rock player 1 wins but the next line after is still invalid input try again and then goes back to asking the user "Make your choice player one"
With the valid inputs I need it to go back to asking "Do you want to continue " instead of automatically restarting the loop.
Last edited on
Ok, this definitely fixes it:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include <iostream>
#include <string>
using namespace std;
int main(){
	char player1, player2;
	string check;
	do
	{

		cout << "Please make your choice Player One [P/R/S]" << endl;
		cin >> player1;

		cout << "Please make your choice Player Two [P/R/S]" << endl;
		cin >> player2;
		
		if (player1 == player2)
			cout << "Nobody wins" << endl;
		else if ((player1 == 'P' && player2 == 'p') || (player1 == 'p' && player2 == 'P'))
			cout << "Nobody wins" << endl;
		else if ((player1 == 'S' && player2 == 's') || (player1 == 's' && player2 == 'S'))
			cout << "Nobody wins" << endl;
		else if ((player1 == 'R' && player2 == 'r') || (player1 == 'r' && player2 == 'R'))
			cout << "Nobody wins" << endl;

		else if ((player1 == 'P' || player1 == 'p') && (player2 == 'R' || player2 == 'r'))
			cout << "Paper covers rock, Player 1 wins" << endl;
		else if ((player1 == 'R' || player1 == 'r') && (player2 == 'P' || player2 == 'p'))
			cout << "Paper covers rock, Player 2 wins" << endl;
		
		else if ((player1 == 'P' || player1 == 'p') && (player2 == 'S' || player2 == 's'))
			cout << "Scissors cut paper, Player 2 wins" << endl;
		else if ((player1 == 'S' || player1 == 's') && (player2 == 'P' || player2 == 'p'))
			cout << "Scissors cut paper, Player 1 wins" << endl;

		else if ((player1 == 'R' || player1 == 'r') && (player2 == 'S' || player2 == 's'))
			cout << "Rock breaks scissors, Player 1 wins" << endl;
		else if ((player1 == 'S' || player1 == 's') && (player2 == 'R' || player2 == 'r'))
			cout << "Rock breaks scissors, Player 2 wins" << endl;
		else
		{
			cout << "Invalid input. Try again." << endl;
			continue;
		}
		cout << "Do you want to continue? [y/n]" << endl;
		cin >> check;
	} while (check != "n");
	system("pause");
	return 0;
}
Ohh! so It was just missing the else if. Thanks so much!
Sure, no problem!
Topic archived. No new replies allowed.