Help please

I know this has been posted a bit, I have looked around quite a bit and I am still having difficulties, so any help is appreciated. Obviously I am new to programming. My issue is that when I run this program (it compiles fine and runs) it allows for the first user to input something, however, after that it displays it for player 2, but then jumps straight to the end and does not allow input for play again loop. I do plan on putting a scoring system into it, however at this point I feel it is more important to actually be able to play it then anything else.
I use Dev C++

I have used system pause for the end just to stare at it..
-why does it jump to the end? how do I stop that so second player can enter their input.
-why does it seem like the loop is not working properly?
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
#include <cstdlib>
#include <iostream>

using namespace std;

int main()
{
    bool Quit = false;
    char Answer;
while (!Quit)
    {
             
    int playerOne_score = 0, playerTwo_score = 0;
    
    int playerOne;
    int playerTwo;
    
    cout << "Player 1: Enter R for rock, P for Paper, and S for Scissors: " << endl;
    cin >> playerOne;
    
    cout << "Player 2: Enter R for rock, P for Paper, and S for Scissors: " << endl;
    cin >> playerTwo;

    if ((playerOne == 'R') || (playerOne == 'r'));
    {
                  if ((playerTwo == 'R') || (playerTwo == 'r')) { 
                  cout << "Tie" << endl; }
                  
                  if ((playerTwo == 'P') || (playerTwo == 'p')) {
                  cout << "Paper beats rock, playerTwo wins" << endl; }

                  if ((playerTwo  == 'S') || (playerTwo == 's')) {
                  cout << "Rock beats Scissors, playerOne wins" << endl; }
}

if ((playerOne == 'P') || (playerOne == 'p'))
{
              if ((playerTwo == 'P') || (playerTwo == 'p')) {
              cout << "tie" << endl; }
              
              if ((playerTwo == 'R') || (playerTwo == 'r')) {
              cout << "Paper beats Rock, playerOne wins" << endl; }
              
              if ((playerTwo == 'S') || (playerTwo == 's')) {
              cout << "Scissors beats Paper, playerTwo wins" << endl; }
}

if ((playerOne == 'S') || (playerOne == 's'))
{
              if  ((playerTwo == 'S') || (playerTwo == 's')) {
              cout << "tie" << endl; }
              
              if ((playerTwo == 'P') || (playerTwo == 'p')) {
              cout << "Scissors beats Paper, playerOne wins" << endl; }
              
              if ((playerTwo == 'R') || (playerTwo == 'r')) {
              cout << "Rock beats Scissors, playerTwo wins" << endl; }  
              
                                             
}

std::cout << "Do you wish to play again? y/n";
std::cin >> Answer;

Answer = std::toupper(Answer);

if (Answer == 'n')
{
              Quit=true;
              }



return 0;
   
}

}

Im new myself to programming but i see that u want to receive input as char but its defined as int
use
1
2
char playerOne;
char playerTwo;

instead of int

also semiculon ; on line 24


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
#include <cstdlib>
#include <iostream>

using namespace std;

int main()
{
	bool Quit = false;
	char Answer;
	while (!Quit)
	{

		int playerOne_score = 0, playerTwo_score = 0;

		char playerOne;
		char playerTwo;

		cout << "Player 1: Enter R for rock, P for Paper, and S for Scissors: " << endl;
		cin >> playerOne;
		//cin.clear();
		//cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');			
		cout << "Player 2: Enter R for rock, P for Paper, and S for Scissors: " << endl;
		cin >> playerTwo;
		//cin.clear();
		//cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');			
		
		if ((playerOne == 'R') || (playerOne == 'r'))
		{
			if ((playerTwo == 'R') || (playerTwo == 'r')) {
				cout << "Tie" << endl;
			}

			if ((playerTwo == 'P') || (playerTwo == 'p')) {
				cout << "Paper beats rock, playerTwo wins" << endl;
			}

			if ((playerTwo == 'S') || (playerTwo == 's')) {
				cout << "Rock beats Scissors, playerOne wins" << endl;
			}
		}

		else if ((playerOne == 'P') || (playerOne == 'p'))
		{
			if ((playerTwo == 'P') || (playerTwo == 'p')) {
				cout << "tie" << endl;
			}

			if ((playerTwo == 'R') || (playerTwo == 'r')) {
				cout << "Paper beats Rock, playerOne wins" << endl;
			}

			if ((playerTwo == 'S') || (playerTwo == 's')) {
				cout << "Scissors beats Paper, playerTwo wins" << endl;
			}
		}

		 else if ((playerOne == 'S') || (playerOne == 's'))
		{
			if ((playerTwo == 'S') || (playerTwo == 's')) {
				cout << "tie" << endl;
			}

			if ((playerTwo == 'P') || (playerTwo == 'p')) {
				cout << "Scissors beats Paper, playerOne wins" << endl;
			}

			if ((playerTwo == 'R') || (playerTwo == 'r')) {
				cout << "Rock beats Scissors, playerTwo wins" << endl;
			}


		}

		cout << "Do you wish to play again? y/n";
		cin >> Answer;

		//Answer = std::toupper(Answer);

		if (Answer == 'n' || Answer == 'N')
		{
			Quit = true;
		}

		

		

	}
	system("pause");
	return 0;
}


Also in
1
2
3
4
5
6
7
8
std::cout << "Do you wish to play again? y/n";
std::cin >> Answer;

Answer = std::toupper(Answer);

if (Answer == 'n'){
    Quit=true;
}

If u enter 'n' or 'N' they are being transformed into N so u never get Answer == 'n' to become true and u cant exit while loop.
use tolower(Answer) instead

or use if (Answer == 'N'){

1 more thing - if u are using namespace std; u dont have to type std::cout - u can type just cout
Last edited on
it is working fine on my compiler.

int playerOne;
int playerTwo;
    

you are trying to make the user to enter R for rock S for scissor and P for paper but you declared the variable that will hold those turns as int. so it will make infinite loop when the user entered R , S or P.
@etrusks

1 more thing - if u are using namespace std; u dont have to type std::cout - u can type just cout


No, doing std::cout is a good thing, using namespace std; is bad.

Also, you commented out some of the good things in the OP's code.

@Morphkp

You used std::toupper at the end of your program, why not use it throughout, and save a lot of typing?

:+)
why is using namespace std; bad?
@etrusks
Same question as you,

@TheIdeasMan
How is using namespace std; bad?
what do you mean @TheIdeasMan ?

I also added in some code in order to try and keep score however, it only keeps track with the first loop and when you play again it did not hold onto the score and starts over again.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
int playerOneWin = 0, playerTwoWin = 0; //inserted after while (quit!)

  if ((playerOne == 'R') || (playerOne == 'r'))
    {
                  if ((playerTwo == 'R') || (playerTwo == 'r')) { 
                  cout << "Tie" << endl; }
                  
                  if ((playerTwo == 'P') || (playerTwo == 'p')) {
                  cout << "Paper beats rock, playerTwo wins" << endl; }
    playerTwoWin++;              // added this and playeronewin++ for each if statement area..
                  if ((playerTwo  == 'S') || (playerTwo == 's')) {
                  cout << "Rock beats Scissors, playerOne wins" << endl; }
    playerOneWin++;            
}

cout << playerOneWin, playerTwoWin;
cout << "\n"; //at the end in order to display the score. 


Any help is appreciated...
Last edited on
There is lots on Google about it, but here is a quick explanation any way:

When one does using namespace std; , it brings in the entire std namespace (which is huge, has all of the STL in it) into the global namespace, which can cause naming conflicts, which defeats the purpose of having namespaces !

It can cause a naming conflict because there are a number of common words in std:: that one might inadvertently use, such as distance, left, right, ratio plus many more.

Ideally, every programmer should put their stuff into it's own namespaces.

If you look at code posted by forum experts like JLBorges as just one example, he always puts std:: before each std thing. So follow his example :+)

Tnx a lot for good explanation :)

I also added in some code in order to try and keep score however, it only keeps track with the first loop and when you play again it did not hold onto the score and starts over again.


U defined int playerOneWin = 0, playerTwoWin = 0; inside of while loop so every time u enter 'y' for - "yes i want to play again"
this part of the code
playerOneWin = 0, playerTwoWin = 0;
is being executed again. So u have to define them before while loop
Topic archived. No new replies allowed.