In need of so much help

I'm trying to write this paper rock scissors game for class using functions. I am just frustrated beyond belief trying to get this program to work. If someone could just at least help me get the fuctions working properly I would be enternally grateful.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
//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 uder quits.
#include <iostream>
#include <cstdlib>
#include <ctype.h>
#include <time.h>
using namespace std;


//******************************************************
//Main Function                                        *
//******************************************************

int main ()
{
   int win, lose, tie, numGames, percent, choice, userChoice, compChoice;

     {
         cout << "Welcome! Want to play a game of Paper Rock Scissors?\n";
         {
         do
           {
           cout << "Press 1 to Play and 2 to Quit.\n";
           cin >> choice;
           }
         while (choice >> 2);

            if (choice == 1)
             {
             displayMenu();
             do
             cin >> userChoice;
             while (userChoice >> 3 or userChoice << 1);
             displayWinner();
             }

          if (choice == 2)
             {
             displayEnd();
             }
         }
     }
return 0;
}


//*******************************************************
//Definition of function displayMenu                    *
//This function displays the menu choices.              *
//*******************************************************

void displayMenu()
{
  int userChoice;
  userChoice = 1 or 2 or 3;

    cout << "1) Paper\n";
    cout << "2) Rock\n";
    cout << "3) Scissors\n";
    cout << "Please make your choice(1-3):\n";
}

//*******************************************************
//Definition of function displayEnd                     *
//This fuction is used to store stats                   *
//*******************************************************

void displayEnd (win, lose, tie)
{
  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";
}

//*******************************************************
//Definition of function displayWinner                  *
//This function is used to determine the winner.        *
//*******************************************************

void displayWinner (userChoice)
{
  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++;
      return;
      }

    else if (userChoice == 2 && compChoice == 3)
      {
      cout << "You have chosen rock and the computer has chosen scissors. You win!\n";
      win++;
      return;
      }

    else if (userChoice == 3 && compChoice == 1)
      {
      cout << "You have chosen scissors and the computer has chosen paper. You win!\n";
      win++;
      return;
      }

    else if (userChoice == 1 && compChoice == 3)
      {
      cout << "You have chosen paper and the computer has chosen scissors. You lose!\n";
      lose++;
      return;
      }
    else if (userChoice == 2 && compChoice == 1)
      {
      cout << "You have chosen rock and the computer has chosen paper. You lose!\n";
      lose++;
      return;
      }

    else if (userChoice == 3 && compChoice == 2)
      {
      cout << "You have chosen scissors and the computer has chosen rock. You lose!\n";
      lose++;
      return;
      }

    else if (userChoice == 1 && compChoice == 1)
      {
      cout << "You have chosen paper and the computer has chosen paper. You tied!\n";
      tie++;
      return;
      }

    else if (userChoice == 2 && compChoice == 2)
      {
      cout << "You have chosen rock and the computer has chosen rock. You tied!\n";
      tie++;
      return;
      }

    else if (userChoice == 3 && compChoice == 3)
      {
      cout << "You have chosen scissors and the computer has chosen scissors. You tied!\n";
      tie++;
      return;
      }

}


In your main() function:
(choice >> 2)
You are bit shifting your choice value by 2. Is this what you want? If you wanted to compare, use '>' or '<'.

Also, what you are trying to do here:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
cout << "Welcome! Want to play a game of Paper Rock Scissors?\n";
         {
         do
           {
           cout << "Press 1 to Play and 2 to Quit.\n";
           cin >> choice;
           }
         while (choice >> 2);

            if (choice == 1)
             {
             displayMenu();
             do
             cin >> userChoice;
             while (userChoice >> 3 or userChoice << 1);
             displayWinner();
             }

          if (choice == 2)
             {
             displayEnd();
             }
         }

There is a much easier way to get in a single character from the user that I use all the time, but I warn you: this is non-standard:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include <iostream>
#incldue <conio.h>

using namespace std;

main()
{
      char ch;
      cout << "Want to play a game of Paper, Rock, Scissors?\n";
      cout << "Press 1 to play, and 2 to quit\n";
      ch=getch();
      
      switch (ch)
      {
             case '1':
                  // Your displayMenu() code here.
                  break;
             
             case '2':
                  //displayEnd() here.
                  break;
             
             default:
                     /* if the user presses something other than 2 or one,
                     display the intro text again. I usually use system("cls"),
                     but I know that's very icky, unless you are strickly working
                     with the console.
                     
                     To display the intro text again, either put it in a function
                     and place it here, along with system("cls"), or use the goto
                     function, and place a label at the top of your intro text,
                     like "MAIN:;", right below a system("cls");                */
      }
}


As you see, very non-standard, but I find this very easy to work with, and I'm sure people can come up with a ISO-standard way of doing this.


Also, in your function:
void displayWinner (userChoice)
Since this is a void function, you can't have a return statement (generally) in this function. And since all of your main code is in if, else if statements, only one 'if' block will run anyways, and the function will end afterwards, so there's no need for a return statement.
Thanks. Ok so I made some improvements but I'm still having problems. When I start the program it works great till you make a choice of paper rock or scissors.... then it ends the program. It doesnt tell the user whether it won or lost or anything. Please help.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
//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;

}
You are using you userChoice variable without assigning it a value.
The reason why it doesn't work is during the displayWinner() function, non of the if, else if statements evaluate to true. For whatever reason, when I debugged your code, the variable userChoice was some insanely large number. Also, I'm questioning your function arguments: it's just int. How is that possible? I thought you had to do something like function(int var, int another), not function(int, int).
LeafyCircuits wrote:
For whatever reason, when I debugged your code, the variable userChoice was some insanely large number

thats because it's uninitialized as I said above.

LeafyCircuits wrote:
I thought you had to do something like function(int var, int another), not function(int, int).

it's ok to do function(int, int) in a function prototype. But not in a function header. The way this program is setup its a bad idea

Last edited on
I got your reply Yanson, but It's night in my timezone, I'll come back to this thread tomorrow....
closed account (3qX21hU5)
I would recommend that you use function(int varName, int varName) in the function prototype and function definition it makes the functions easier to understand sometimes but as Yanson said you only need to have the variables type in the function prototype.


1
2
3
4
5
6
7
8
9
10
11
12
13
// Function prototype
int addStuff(int one, int two);

int main()
{
    // Stuff
}

// Function definition
int addStuff(int one, int two)
{
    return one + two;
}


It looks like you don't really understand how functions work I would recommend you check out the tutorial here on them or do some reading on them in whatever resources you have. If you have any questions feel free to ask.


For example lets take a look at your display function

1
2
3
4
5
6
7
8
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";
}


1) On line 4 you assign win / numGames to percent. The problem is though that there is nothing in win or numGames. They are uninitialized variables so you will get undefined behavior.

2) On line 5 you are doing the same as on line 4.


You need to use the parameters to pass in variables so that you can do your calculations on them. I won't go into depth on how to do this but I will talk alittle about it.

Here is a example.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
void displayInfo(string userName, int userAge)
{
    cout << "Your name is: " << userName << endl;
    cout << "Your age is: " << userAge << endl;
}

int main()
{
    string myName("Brandon");
    int myAge = 23;
    
    // Notice how I pass in the variables into the function.
    displayInfo(myName, myAge);
}


Now the first thing to look at is how I declare the parameters userName and userAge. Whenever you want to work with data from other parts of your program in a function you will need to pass it into the function as a argument. So in my example I wanted to print the variables myName and myAge so I passed them into the parameters userName and userAge when I called the function in main. That is how you work with variables from your program in a function.

How it works is the function takes the myName variable and makes a copy of it and puts that copy in userName so that we can run calculations on it inside of the function. So basically we treat userName as if it were myName because that is what we passed to the function.

Not sure if this is clear or not it is kinda late where I am and getting tired ;p But hopefully it helped a little. But read up on functions some more you almost got them but could still use a little bit more information on them. If you run into any problems or need help with anything just let us know and we would be glad to help.

And don't use goto's, and especially not with a label called main: !! That was bad advice from LeafyCircuits . There are some situations where goto might be warranted, but this isn't one of them, and they ought to be very rare.

Also, there is no need for a switch because we only have 2 options, so this code would be better:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
bool Quit = false;

while (!Quit) {
   //your code here

   //ask user if they want to quit
   //get input
   
  // convert to upper case with toupper function 
  //user wants to quit
    if (Answer == 'Q') {
         Quit = true;
         //could use return 0; here otherwise execution continues after the end of the while loop
    }
}


A switch is good for a menu type situation, in which case you would put it inside the loop shown above, along with a function that shows the menu.

Hope all goes well :)

Edit:

I really dislike do loops - beginners seem to like them, but IMO just because they always execute once is not a reason to prefer them over a for or while loop. All 3 forms of loop can be re-written into one of the other forms, so I prefer to use a do loop only if I really have to.
Last edited on
Topic archived. No new replies allowed.