Ok I am writing a Rock Paper Scissors game for my c++ class. I have done most of it, but now I am stuck. I need to keep an overall score for the computer wins, user wins, and ties for as long as the user wants to keep playing. After you play the program needs to read the score and then ask if you want to play again. Everything works, except the score resets after every game. Here is my code so far...

#include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std;
char getUserChoice(char);
char choice;
int main()
{
  char again;
  do {
    char comp;
    int computer = 0;
    int player = 0;
    int tie = 0;
    srandom(time(0));
    int n = random() % 3;
    n++;

if (n == 1) {
      comp = 'R';
    } else if (n == 2) {
      comp = 'P';
    } else {
      comp = 'S';
    }

if (comp == getUserChoice(choice)) {
      cout << "It's a tie.\n";
      tie += 1;
    } else if ((comp == 'R') && (choice == 'S')) {
      cout << "Rock smashes scissors. You Lose.\n";
      computer += 1;
    } else if ((comp == 'P') && (choice == 'R')) {
      cout << "Paper covors rock. You Lose.\n";
      computer += 1;
    } else if ((comp == 'S') && (choice == 'P')) {
      cout << "Scissors cut paper. You Lose.\n";
      computer += 1;
    } else if ((comp == 'R') && (choice == 'P')) {
      cout << "Paper covors rock. You Win.\n";
      player += 1;
    } else if ((comp == 'P') && (choice == 'S')) {
      cout << "Scissors cut paper.You Win.\n";
      player += 1;
    } else if ((comp == 'S') && (choice == 'R')) {
      cout << "Rock smashes scissors.You Win.\n";
      player += 1;
    }
    cout << "Computer wins: " << computer << endl << "User wins: " << player << endl << "Ties: "
         << tie << endl;

    cout << "Play again?(Yes or No): ";
    cin >> again;
    cin.ignore(1000, '\n');
    again = toupper(again);
  } while (again == 'Y')
      ;
  return(0);
}

char getUserChoice(char)
{
  cout << "Please enter Rock, Paper, or Scissors: ";
  cin >> choice;
  cin.ignore(1000, '\n');
  choice = toupper(choice);
  return(choice);
}

Your problem is:

do {
int computer = 0;
int player = 0;
int tie = 0;

That code is in the game loop... so every time the user wants to play again, all the scores get set back to '0'. Just move the declaration of the variables outside of the loop, and initialize them outside the loop as well. Then, inside the loop increment like you are already doing.

Wow, well now I feel bad for working on this problem for like an hour with no results lol. It worked thanks.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.