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;

}

You are not passing any variables to your functions. Since you want to update what the values of the variables are you need to pass them by reference. You also need to get rid of the declerations of win, lose and tie in your functions. displayWinner() should look like this

void displayWinner(int & win, int & lose, int & tie)
{
    int compChoice, userChoice;  // do not declare win, lose and tie since they are function arguments
    // rest of your code here
}

and on line 131 you would call it like this

displayWinner(win, lose, tie);

Line 99 should be

void displayEnd(int win, int lose, int tie, int numGames)
{
    // rest of your code here
}

and on line 135 call displayEnd() like this

displayEnd(win, lose, tie, numGames);

You also need to increment numGames every time you call displayWinner(). Using goto is a big nono as well. anything thay you use a goto for can be replaced with a while loop. I might have missed a couple of things but make the changes I suggested and see what you get.

Ok I made the changes you suggested and I believe we are getting somewhere. Please excuse the fact that I'm ignoring the goto statement till I can get the stats working right first. After making the changes I get some bizare stats.

So far your have played 3 sessions and have won -4195188, tied -4195196, and lost 5 for a winning percent of -1398396%.

Only played 1 game that session.

//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";
"Project5.cpp" 140L, 3820Cc
//Nicholas Rechtien
//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, int & lose, int & tie)
{
  int compChoice, userChoice;
  compChoice = rand() % 3 + 1;

      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 win, int lose, int tie, int numGames)
{
  int percent;
  percent = win / numGames;

     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(win, lose, tie);
        goto second;

      case '2':
        displayEnd(win, lose, tie, numGames);
        break;

      default:
        goto main;
    }
   }
return 0;

}

You need to initialize your variables in main() - set them to 0 as starting values.

you also still need to increment numGames when you call displayWinner()

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.