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);
}