5 Replies - 256 Views - Last Post: 19 April 2013 - 09:47 PM Rate Topic: -----

#1 Nick007  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 19-April 13

Paper Rock Scissors program help

Posted 19 April 2013 - 08:45 PM

Hey folks. For this homework assignment I have to create a Paper Rock Scissors game using functions. I've got the code written and I'm finally not getting any compilation errors. But when I run the program, after I put in my choice for paper rock scissors, it ends the program instead of going through the function. Any help is appreciated. Thanks.

One person said that I am using userChoice variable without assigning it a value. I am confused by this becuase it looks like it gets assigned right. I never got a reply back from guy so I'm lost on that.

//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)
{
  int win, lose, tie, compChoice, userChoice;
  compChoice = rand() % 3 + 1;

      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;
  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;
   win = 0;
   lose = 0;
   tie = 0;
   char choice;


   {main:
      cout << "Welcome! Want to play a game of Paper Rock Scissors?\n";
      cout << "Press 1 to play, and 2 to quit.\n";
      cin >> choice;

   switch (choice)
    {
      case '1':
        displayMenu();
        cin >> userChoice;
        displayWinner(userChoice);
        break;

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

      default:
        goto main;
    }
   }
return 0;

}



Is This A Good Question/Topic? 0
  • +

Replies To: Paper Rock Scissors program help

#2 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1072
  • View blog
  • Posts: 4,532
  • Joined: 09-June 09

Re: Paper Rock Scissors program help

Posted 19 April 2013 - 08:51 PM

You need to pass your choice to the displayWinner function, you are just using uninitalize variables which contain garbage.

void displayWinner (int) /* should be void displayWinner(int userChoice) */




Also, try not to use goto. Using a proper loop construct to achieve the same result.
Was This Post Helpful? 0
  • +
  • -

#3 Nick007  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 19-April 13

Re: Paper Rock Scissors program help

Posted 19 April 2013 - 09:08 PM

Quote

When I do that I get the error declaration of 'int userChoice' shadows a parameter

Quote

void displayWinner (int) /* should be void displayWinner(int userChoice) */


When I do this I get the error declaration of 'int userChoice' shadows a p

When I do that I get the error declaration of 'int userChoice' shadows a parameter

Quote

void displayWinner (int) /* should be void displayWinner(int userChoice) */


When I do this I get the error declaration of 'int userChoice' shadows a parameter
Was This Post Helpful? 0
  • +
  • -

#4 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1072
  • View blog
  • Posts: 4,532
  • Joined: 09-June 09

Re: Paper Rock Scissors program help

Posted 19 April 2013 - 09:11 PM

The input you recieved in your main function needs to go into your displayWinner function. To do this, you pass it in as a parameter to that function (which you correctly do from main)

displayWinner(userChoice);



When you call displayWinner, program execution will jump to the top of the displayWinner function. The value of userChoice in main will be copied into the parameter of displayWinner.
void displayWinner (int userChoice) /* <--- userChoice from main gets copied into userChoice here */



You don't need to redeclare (int userChoice) within the displayWinner function, it is already being passed as a parameter (hence why you get a shadow parameter error)

This post has been edited by jjl: 19 April 2013 - 09:12 PM

Was This Post Helpful? 0
  • +
  • -

#5 Nick007  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 19-April 13

Re: Paper Rock Scissors program help

Posted 19 April 2013 - 09:24 PM

Ok Thanks man. I finally got it working.... well mostly. The only problem I am having now is when I end the program, the stats are way messed up.

I get the output that says "So far your have played 17194274 sessions and have won 139265, tied 138420, and lost 16916589 for a winning percent of 69632%."

I knew that the percentage equation was wrong or at least wasnt going to put in the right number, (just put it in till I got the program working properly), but I was expecting the games played, win, loss, and tied variables to work right. Dont suppose you have any advice for that?
Was This Post Helpful? 0
  • +
  • -

#6 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1072
  • View blog
  • Posts: 4,532
  • Joined: 09-June 09

Re: Paper Rock Scissors program help

Posted 19 April 2013 - 09:47 PM

Post your updated code.

But by a quick glance, you need to initalize your variables in displayWinner before you increment them. Declaring a variable does not initialize it to zero, the variable will contain garbage (usually from a previous process).

i.e.
void displayWinner(int userChoice) {
   int win; /* win contains garbage */

   win++; /* what's garbage + 1? That's right, more garbage */



A good rule of thumb is to always initialize your variables when you declare them.

This post has been edited by jjl: 19 April 2013 - 09:50 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1