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?
#include <cstdlib>
#include <iostream>
usingnamespace 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;
}
}
elseif ((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;
}
}
elseif ((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 usingnamespace std; u dont have to type std::cout - u can type just cout
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.
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.
There is lots on Google about it, but here is a quick explanation any way:
When one does usingnamespace 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 :+)
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