i need to add yes or that will restart the loop or exit

After identifying the winner, the system should ask if the user want another game (If the user select YES ā€˜Yā€™ then new set of game should be perform otherwise if the user select NO ā€˜Nā€™ then the system will exit). pls help, i dont know what code to add
this is my code:

#include <iostream>

using namespace std;

int main()

{

char pone;

char ptwo;

cout << "\nWelcome Players!";

cout << "\nLet's start the game";

cout << "\nSelect your Pick...Pres [R] Rock; [S]; [P] Paper";

cout << "\nPlayer One, Please Enter your Pick:";

cin >> pone;

{
cout << "\nPlayer Two, Please Enter your Pick:";
cin >> ptwo;
}
if (pone == 'r' || pone == 'R') {
if (ptwo == 'r' || ptwo == 'R') {
cout << "It's a draw!" << endl;
}
}

if (pone == 'p' || pone == 'P')

{
if (ptwo == 'p' || ptwo == 'P') {
cout << "It's a draw!" << endl;
}
}

if (pone == 's' || pone == 'S')

{

if (ptwo == 's' || ptwo == 'S')

{

cout << "It's a draw!" << endl;
}
}

if (pone == 'p' || pone == 'P')

{
if (ptwo == 'r' || ptwo == 'R')

{
cout << "Player 1 wins, Paper covers Rock." << endl;
}
}

if (pone == 'r' || pone == 'R')

{

if (ptwo == 'p' || ptwo == 'P')

{

cout << "Player 2 wins, Paper covers Rock." << endl;
}
}

if (pone == 'p' || pone == 'P')

{

if (ptwo == 's' || ptwo == 'S')

{

cout << "Player 2 wins, Scissors cut Paper." << endl;
}
}

if (pone == 's' || pone == 'S')

{

if (ptwo == 'p' || ptwo == 'P')

{

cout << "Player 1 wins, Scissors cut Paper." << endl;
}
}

if (pone == 'r' || pone == 'R')

{
if (ptwo == 's' || ptwo == 'S')

{
cout << "Player 1 wins,Rock crushes Scissors." << endl;
}
}

if (pone == 's' || pone == 'S')

{

if (ptwo == 'r' || ptwo == 'R')

{

cout << "Player 2 wins, Rock crushes Scissors." << endl;
}
}

else

{

cout << "Invalid Input";
}

return 0;
}
Hi Grumpychael,

So to add a yes or now to the loop, maybe add a loop in the first place?

I think in your case a do while or while loop is what you need : https://en.cppreference.com/w/cpp/language/while
can u please provide me the codes so i can understand it clearly pls
Hi,

Ive added in 2 loops for you. One for the main running of the program. The loop starts after the program has
std::cout << "\nLet's start the game";


The loop repeats if a boolean variable is true. I have initialised a boolean variable called again and put that as the condition for the while loop. So if that variable is true that loop is ran.

To break out of this loop the variable again will need to be set equal to false. I have done this at the end of the game using another while loop. Here the condition for the loop is done by a variable called invalidinput. When this variable is true then a loop asking the user if they want to play again is ran. If they give a valid input (either "Y" or "N") then that loop will be broken and if they typed Y the main loop will start again and they'll be able to play the game again or if they type N then the loop will break and the main loop will break aswell as the variable
again
is set to false and the program will end.


I have also changed the if statments to become one big if...else if...else statement:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
if()
{
...
}
else if()
{
.....
}
else if()
{
.....
}
.....
.....
else
{
...
}


I did this by removing your nested if statements and putting the conditions on 1 if statement line (Nested if: When an if statement is within another if statement). I did this by using a little trick called "tolower()" This takes a char value and converts it to its lowercase equivalent. If the char is already its lowercase equivalent then it stays like that. This removes the need to check if the input is the capitalised version or the lowercase version of R, P, S by converting to lowercase and requiring one check.


Because I was able to do this I was then able to reduce the inner nested ifs statement to the exact same and put it on the same line as the outer nested if statement and use the "&&" operand rather than the "||" operand. This means that both conditions need to be true for it to enter into the if statement. ("&&" Means "AND", "||" means OR).

Example:

This

1
2
3
4
5
6
7
if (pone == 'r' || pone == 'R')
{
    if (ptwo == 'r' || ptwo == 'R')
    {
        cout << "It's a draw!" << endl;
     }
}


Becomes

1
2
3
4
if (tolower(pone) == 'r' && tolower(ptwo) == 'r') 
{
	std::cout << "It's a draw!" << std::endl;
}



I also added some more dialog to fit with the new loops. These can be changed as you wish and formatted to look neater. I have posted the full code below.

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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#include "pch.h"
#include <iostream>



int main()
{
	bool invalidinput;
	bool again = true;
	char pone;

	char ptwo;

	std::cout << "\nWelcome Players!";

	std::cout << "\nLet's start the game";
	while (again)
	{
		std::cout << "\nSelect your Pick...Pres [R] Rock; [S] Scissors; [P] Paper";

		std::cout << "\nPlayer One, Please Enter your Pick:";

		std::cin >> pone;

		{
			std::cout << "\nPlayer Two, Please Enter your Pick:";
			std::cin >> ptwo;
		}
                pone = tolower(pone);
                ptwo = tolower(ptwo);
		if (pone == 'r' && ptwo== 'r') 
		{
			std::cout << "It's a draw!" << std::endl;
		}
		else if (pone == 'p' && ptwo == 'p')
		{
			std::cout << "It's a draw!" << std::endl;
		}
		else if (pone == 's' || ptwo == 's')
		{
			std::cout << "It's a draw!" << std::endl;
		}
		else if (pone == 'p' && ptwo == 'r')
		{
			std::cout << "Player 1 wins, Paper covers Rock." << std::endl;
		}
		else if (pone == 'r' && ptwo == 'p')
		{
			std::cout << "Player 2 wins, Paper covers Rock." << std::endl;
		}
		else if (pone == 'p' && ptwo == 's')
		{
			std::cout << "Player 2 wins, Scissors cut Paper." << std::endl;
		}
		else if (pone == 's' && ptwo == 'p')
		{
			std::cout << "Player 1 wins, Scissors cut Paper." << std::endl;
		}
		else if (pone == 'r' && tolower(ptwo) == 's')
		{
			std::cout << "Player 1 wins,Rock crushes Scissors." << std::endl;
		}
		else if (pone == 's' && ptwo == 'r')
		{
			std::cout << "Player 2 wins, Rock crushes Scissors." << std::endl;
		}
		else
		{

			std::cout << "Invalid Input";
		}


		invalidinput = true;
		std::cout << "\nThat was so much fun. Do you want to play again? ";
		while (invalidinput)
		{
			std::cout << "\nPress [Y] fo yes and [N] for no";
			std::cin >> pone;
			if (tolower(pone) == 'n')
			{
				again = false;
				invalidinput = false;
			}
			else if (tolower(pone) == 'y')
			{
				invalidinput = false;
			}
			else
			{
				std::cout << "Invalid Input. Try again.";
			}
		}
	}

	std::cout << "Thanks for playing.";
	return 0;
}
Last edited on
Consider calling tolower once only for each input after input on line 28.

The code would be easier to read with a switch statement.
Last edited on
Thanks for catching that TheIdeasMan.

Just out of curiosity for my own knowledge i thought a case statement would take a single char character as its input rather than 2 inputs. (pone and ptwo). If you were to implement it with a case statement how would you go about it?


Grumpychael I have changed the above code so tolower is not called on every comparison, which was my mistake for not doing previously.
switches specifically work on integer cases only. You have to find a way to boil your cases down to a single integer for it to work, making them severely limited.

you can force fit stuff --- if pone and ptwo were 32 bit ints, you could jack them into a 64 bit int, those kind of tricks, or you could cook up some sort of multi-state variable (1 means pone is x and p2 is y, 2 means pone is z and p2 is y, ... etc).

So, you could do it, since these are characters.
short switcher = 0;
switcher = pone*256+ptwo;
switch (switcher)
case 769 : //(pone is 3 and ptwo is 1)

is this useful? :P

IF YOU DO THIS I can't recommend enough using an enum to NAME the cases, eg
enum
{
threeandone = 769,
a_and_b = 'a'*256+'b',
etc
}
then you get
case
y_and_n: //readable! whatever letter combos you need to cover
y_and_y:

also its really nice to just toupper or tolower everything rather than try to compare all the possible mixed case answers.
if (ptwo == 'p' || ptwo == 'P')
should be
if(tolower(ptwo) == 'p') //covers both
Last edited on
Topic archived. No new replies allowed.