Always goes to error

I'm pretty new to c++ and I've been trying to create this rock paper scissors game where you can enter the word for what you chose and play against a computer. Although I am entering either rock, paper, or scissors exactly, it always goes to default in the last switch statement and says error. I have no idea what the problem is. Anybody here know what the error may be? I'm using online gbd to compile and execute the code.

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
#include <iostream>
#include <stdlib.h>
#include <ctime>
#include <string>

int main()
{

srand(time(NULL));

int rando = rand() % 3;
int pNumb;
std::string pTurn ("");
std::string cpTurn ("");
std::string p1 ("The computer chose ");
std::string p2 (" and you chose ");
std::string win (" so you win!\n");
std::string lose (" so you lost :(.\n");
std::string error ("error\n");
std::string tied (" so you tied!\n");
std::string r ("rock");
std::string p ("paper");
std::string s ("scissors");
int numbDiff = rando - pNumb;

switch (rando) {
    case 0: cpTurn = r;
            break;
    case 1: cpTurn = p;
            break;
    case 2: cpTurn = s;
            break;
    default: cpTurn = error;
            break;
}

std::cout << "Chose rock, paper, or scissors!(exactly as stated)\n";
std::cin >> pTurn;

if (pTurn == r) {
    pNumb = 0;
}
else if (pTurn == p) {
    pNumb = 1;
}
else if (pTurn == s) {
    pNumb = 2;
}

switch (numbDiff) {
    case -2: std::cout << p1 << cpTurn << p2 << pTurn << lose;
            break;
    case -1: std::cout << p1 << cpTurn << p2 << pTurn << win;
            break;
    case 0: std::cout << p1 << cpTurn << p2 << pTurn << tied;
            break;
    case 1: std::cout << p1 << cpTurn << p2 << pTurn << lose;
            break;
    case 2: std::cout << p1 << cpTurn << p2 << pTurn << win;
            break;
    default: std::cout << error;
            break;
}


}
On line 24, what do you think the value of pNumb is?

Always compile with warnings enabled.
 In function 'int main()':
24:24: warning: 'pNumb' may be used uninitialized in this function [-Wmaybe-uninitialized]
Last edited on
Also, in line 37 you ask the user to enter
Chose rock, paper, or scissors!(exactly as stated)
, but then check for r, p, or s.

Surely you want to ask something like:
1
2
std::cout << "Choose (r)ock, (p)aper, or (s)cissors! (r, p, or s)\n";
std::cin >> pTurn;

See posts below. I hadn't realized the strings for "rock", "paper" and "scissors" are defined at the top of the program.
Last edited on
Oh, good catch. And also, note that on lines 40-48, you assign pNumb a new value, but then you never actually use pNumb again. If you want numbDiff to have an updated value, you need to assign to numbDiff after you assign to pNumb.
Last edited on
Eraun, I'm curious, did you think that numbDiff = rando - pNumb would cause numbDiff to be updated when pNumb was updated? I've seen many beginners assign values to variables too early and I wonder if they think that C++ programs work like spreadsheet formulas, where the value automatically updates when a component of the formula updates.

but then check for r, p, or s.
. That's okay. r, p, and s are strings with the values "rock" "paper" and "scissors". See lines 21-23
@dhayden - Oh yeah, yeah. Of course; didn't notice that up top, although it does seem a slightly roundabout way of doing it. I'll amend my post.

Alright, so I put the numbDiff = rando - pNumb after the if then statement where pNumb is set. That fixed it. Thanks guys.
Topic archived. No new replies allowed.