Rock, Paper, Scissors game

Good evening all,

Can anyone point me in the right direction with my code for a rock, paper, scissors game?

#include <iostream>
#include <string>
#include <cstdlib>

using namespace std;

int main ()

{

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

string choice;

int numberOfTies = 0;

int numberOfWins = 0;

int numberOfLosses = 0;

cout << "Choose: Rock, Paper, or Scissors:" << endl;

cin >> choice;

cout << "Computer choice: " << compChoice << endl; //How do I get the code to show rock, paper, or scissors instead of 1, 2, or 3??

cout << "" << endl;

if (choice == "Rock")

{

if (compChoice == 1)

cout << "It's a tie!" << endl;

else if (compChoice == 2)

cout << "Paper beats rock! Sorry, you lose!" << endl;

else if (compChoice == 3)

cout << "Rock beats scissors! You win! " << endl;

}

if (choice == "Paper")

{

if (compChoice == 1)

cout << "It's a tie!\n";

else if (compChoice == 2)

cout << "Scissors cuts Paper! Sorry, you lose.\n";

else if (compChoice == 3)

cout << "Paper beats Rock! You win!\n";

}

if (choice == "Scissors")

{

if (compChoice == 1)

cout << "It's a tie!\n";

else if (compChoice == 2)

cout << "Rock beats Scissors! Sorry, you lose!\n";

else if (compChoice == 3)

cout << "Scissors cuts Paper! You win!\n";

}

cout << "Would you like to play again? (Y/N)" << endl;

cin >> choice;

cout << "" << endl;

if (choice == "Y")

{

return main ();

}

else

{

cout << "You had " << numberOfTies << " tie/s\n";

cout << "You had " << numberOfWins << " win/s" << endl;

cout << "You had " << numberOfLosses << " loss/es" << endl;

cout << "" << endl;

cout << "Thank you for playing. Have a nice day!\n";

}

return 0;

}

So, my questions:

1. How do I get the code to display the computer choice as rock, paper, or scissors instead of 1, 2, 3?

2. How do I keep track of the ties, wins, and losses?

3. Why is it that when I input "N" when it asks if I want to play again, the records for wins, losses, and ties all show 0?

I'm really new at this, so any help is appreciated! Thank you!
By the way please use coding tags: http://www.cplusplus.com/articles/jEywvCM9/
It helps the reader ;)

1) Just use if statements to print what the computer had chosen.
1
2
3
4
5
6
7
cout << "Computer choice: ";
if(compChoice ==1)
   cout<<"Rock\n";
else if(compChoice==2)
   cout<<"Paper\n";
else
   cout<<"Scissors\n";


2) After printing "You lose" increment the variable holding losses by one, likewise for the wins and ties.
The variables will then hold number of wins, losses and ties, so you can display them like you are intending to.

3) They show 0 because you never incremented the variables ;p!

Also I don't think return main(); is good practice instead enclose the entire thing with a while loop whose condition becomes false when player does not want to play. ;)
Last edited on
Thank you @Nwb, that worked!!!
Topic archived. No new replies allowed.