I need a little help with a rock paper scissors game

its not supposed to be rock paper scissors yet, were just making something similar. But im having a problem with an infinite loop and i was wondering if someone could make heads or tails of this

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
 #include <iostream>
#include <string>
using namespace std;
int main()
{
	int Guess;
	char Cont;
	bool loop=true;
	cout<<"Let's play Rock, Paper, Scissors"<<endl;
	cout<<"Enter 1 for Rock, 2 for Paper, 3 for Scissors"<<endl;
	
	
	while (loop==true)
	{
		cout<<"Please Guess"<<endl;
		cin>>Guess;
		switch(Guess)
			{
				case 1:
				cout<<"You chose rock"<<endl;
				break;
				case 2:
				cout<<"You chose paper"<<endl;
				break;
				case 3:
				cout<<"You chose scissors"<<endl;
				break;
				default:
				cout<<Guess<<" is not a valid choice"<<endl;
			}
		cout<<"Would you like to play again (Y for yes, N for no)?"<<endl;
		cin>>Cont;
		if (Cont=='y') 
			loop=true;
		}
	return(0);
}

You never set loop to false anywhere in your program. It is not possible for the loop to end. Maybe on lines 33 and 34 you meant == 'n' and loop = false;, but even then why not just use the break; statement?
Well, you have condition "while loop is true" and there is nothing which would change it to false.

You never set loop to false anywhere in your program. It is not possible for the loop to end. Maybe on lines 33 and 34 you meant == 'n' and loop = false;, but even then why not just use the break; statement?[quote]



Where would i use a break statement?
Last edited on
closed account (EwCjE3v7)
@David Look at lines 31 and down, If i type N, will it actually get out of the loop? No it wont. I will go back to the top as if I had typed a y and == is case sensitive so y is not Y.

You could have an else statement after the if with a break statement and that should fix it.
Topic archived. No new replies allowed.