Creating Rock Paper Scissors game.

closed account (1R91hbRD)
Why am I getting infinite loops for each user input? PLEASE HELP!
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
/******************************************************************************************
**When the program begins, a random number in the range of 1 through 3 is generated. 
**If the number is 1, then the computer has chosen rock.  
**If the number is 2, then the computer has chosen paper.  
**If the number is 3, then the computer has chosen scissors.  
**Then the user enters his or her choice of "rock", "paper", or "scissors" at the keyboard. 
*******************************************************************************************/

#include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std;

int main()
{
	system("color D5");
	srand(time(NULL)); //seeds the generator

int rand_num;
	rand_num = rand()%3+1;

int choice;
cout << "* * * * * * * * * * * * * * * * * * * * * * * * * * * " << endl
	 << "Welcome to Megatron's Rock, Paper, Scissors game." << endl
	 << "* * * * * * * * * * * * * * * * * * * * * * * * * * * " << endl <<endl
	 << " Would you like to play" <<endl
	 << "(Y for yes and N for NO)" << endl;
char play;
cin >> play;
while (play == 'y' || play == 'Y' )
{
cout << "Can you beat Megratron?" << endl
	 << "(Type 0 for Rock, 1 for Paper or  2 for Scissors)" << endl << endl;
cin >> choice;
// get choice from the user
//compare choice to the rand
//display results
//play again

while (choice == 1) 
    {
             
              if (rand_num == 2)
              cout << "Paper beats rock! Sorry, you lose!\n\n\n\n";
              else if (rand_num == 3)
              cout << "Rock beats scissors! You win!\n\n\n\n"; 
			  else if (rand_num == 1)
			  cout << "It's a tie!\n\n\n\n";
    }
    
    if (choice == 2)
    {
			   
               if (rand_num == 2)
               cout << "Paper beats rock! You win!\n\n\n\n";
               else if (rand_num == 3)
               cout << "Scissors beat paper! Megatron wins!\n\n\n\n";
			   else if (rand_num == 1)
			   cout << "It's a tie!\n\n\n\n";

   }
   
   if (choice == 3)
   {
              
              if (rand_num == 2)
              cout << "Scissors beat paper! You win!\n\n\n\n";
              else if (rand_num == 3)
              cout << "Rock beats scissors! Sorry, you lose!\n\n\n\n";
			  else if (rand_num == 1)
			  cout << "It's a tie!\n\n\n\n";
   }
   else 
	   cout << "invalid input, please try again" <<endl;
}
	cout << "program now closing" <<endl;

   return 0;
}
The loop will run as long as play is y or Y. Where, inside that loop, can play be changed?

Also, is this while (choice == 1) meant to be if (choice == 1) ?
Topic archived. No new replies allowed.