In this program I am writing for class, I have to make a Paper Rock Scissor game using functions. I got everything written and everything seems to work until I quit the program and it's supposed to display the stats. It seems that the variables arent storing and being passed correctly. I've been trying and retrying different things so much that I've lost track. IF anyone could show me what I am doing wrong and help me get the stats working I would greatly appreciate it! Thanks.
//This program is a Rock, Paper, Scissor game. It will ask you if you want to
//play the game, and if yes, will have you make a choice and determine the
//winner and keep score until the user quits.
#include <iostream>
#include <cstdlib>
#include <ctype.h>
#include <time.h>
using namespace std;
//*******************************************************
//Definition of function displayMenu *
//This function displays the menu choices. *
//*******************************************************
void displayMenu()
{
int userChoice;
cout << "1) Paper\n";
cout << "2) Rock\n";
cout << "3) Scissors\n";
cout << "Please make your choice(1-3):\n";
}
//*******************************************************
//Definition of function displayWinner *
//This function is used to determine the winner. *
//*******************************************************
void displayWinner ()
{
int win, lose, tie, compChoice, userChoice;
compChoice = rand() % 3 + 1;
win = 0;
lose = 0;
tie = 0;
cin >> userChoice;
if (userChoice == 1 && compChoice == 2)
{
cout << "You have chosen paper and the computer has chosen rock. You win!\n";
win++;
}
else if (userChoice == 2 && compChoice == 3)
{
cout << "You have chosen rock and the computer has chosen scissors. You win!\n";
win++;
}
else if (userChoice == 3 && compChoice == 1)
{
cout << "You have chosen scissors and the computer has chosen paper. You win!\n";
win++;
}
else if (userChoice == 1 && compChoice == 3)
{
cout << "You have chosen paper and the computer has chosen scissors. You lose!\n";
lose++;
}
else if (userChoice == 2 && compChoice == 1)
{
cout << "You have chosen rock and the computer has chosen paper. You lose!\n";
lose++;
}
else if (userChoice == 3 && compChoice == 2)
{
cout << "You have chosen scissors and the computer has chosen rock. You lose!\n";
lose++;
}
else if (userChoice == 1 && compChoice == 1)
{
cout << "You have chosen paper and the computer has chosen paper. You tied!\n";
tie++;
}
else if (userChoice == 2 && compChoice == 2)
{
cout << "You have chosen rock and the computer has chosen rock. You tied!\n";
tie++;
}
else if (userChoice == 3 && compChoice == 3)
{
cout << "You have chosen scissors and the computer has chosen scissors. You tied!\n";
tie++;
}
}
//*******************************************************
//Definition of function displayEnd *
//This fuction is used to store stats *
//*******************************************************
void displayEnd (int, int, int)
{
int win, lose, tie, numGames, percent;
win = 0;
lose = 0;
tie = 0;
percent = win / numGames;
numGames = win + lose + tie;
cout << "So far your have played " << numGames << " sessions and have won " << win << ", tied " << tie << ", and lost " << lose << " for a winning percent of " << percent << "%.\n";
}
//******************************************************
//Main Function *
//******************************************************
int main ()
{
int win, lose, tie, numGames, percent, userChoice, compChoice;
char choice;
{main:
cout << "Welcome! Want to play a game of Paper Rock Scissors?\n";
second:
cout << "Press 1 to play, and 2 to quit.\n";
cin >> choice;
switch (choice)
{
case '1':
displayMenu();
displayWinner();
goto second;
case '2':
displayEnd(win, lose, tie);
break;
default:
goto main;
}
}
return 0;
}