Program skips "if statement" if true?

Thanks Peter87, kemort, and TheIdeasMan! I'll keep those pointers in mind when I look to improve my program. As of now, I'll submit what has already been done.
Last edited on
waranthem wrote:
Trying to write a program that focuses on a roulette simulation. For some reason, the program skips the "if statement" if true, and always executes the "else statement".

For example:

1 (Red) or 2 (Black)

Lands on Red 9
You choose 1. You lost.

^ I want it to correctly display "You won" instead as the entered value and generated number color matches.

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

int wheel ();
int slotcolor (int);

int choice;
int bet = 1, oddeven = 0, number = 1, ball, color;

bool menu = true;

int main 
{

while (menu)
{
   switch (choice)
        {

            case 1:
                cout << "\n" << "Red or Black (1 or 2)? " << "\n";
                cin >> bet;

                cout << "\n" << "1 (Red) or 2 (Black)" << "\n";

                ball = wheel();
                color = slotcolor (ball);

                if (bet == color)
                {
                    cout << "You choose " << bet << ". You won!" << "\n";
                }
                else
                {
                    cout << "You choose " << bet << ". You lost." << "\n";
                }
                break;

              case 2:
              .....
        }
}

}

int wheel ()
{
    int num;
    num = rand () % 37;
    return num;
}

int slotcolor (int num)
{
    int color = 1, value;
    value = num % 2;

    if (value == 1)
    {
        if ( ((num >= 1) && (num <= 9)) || ((num >= 19) && (num <= 27)) )
        {
            color = 1;
        }

        if ( ((num >= 11) && (num <= 17)) || ((num >= 29) && (num <= 35)) )
        {
            color = 2;
        }
    }
    else
    {
        if (num == 0)
        {
            color = 3;
        }

        if ( ((num >= 2) && (num <= 10)) || ((num >= 20) && (num <= 28)) )
        {
            color = 2;
        }

        if ( ((num >= 12) && (num <= 18)) || ((num >= 30) && (num <= 36)) )
        {
            color = 1;
        }
    }

    switch (color)
    {
        case 1:
            cout << "\n" << "Lands on Red " << num << "\n";
            break;
        case 2:
            cout << "\n" << "Lands on Black " << num << "\n";
            break;
        case 3:
            cout << "\n" << "Lands on Green " << num << "\n";
            break;
    }
}


You never return anything from the slotcolor function. See if you can turn up your compiler's warning level because compilers are often able to warn about simple errors like this.
Last edited on
How would I return the slotcolor function so that it displays whether or not the user won?

Last edited on
closed account (48T7M4Gy)
always executes the "else statement"

I think your value ranges overlapping might be the issue.

A simpler way of determining the color is to use a simple array like:

int position[37] = { 3,1,1,1, ... etc};

So if it lands on position[12] then the color will be int color = position[12];

This can then feed the switch, no if's needed :)
Last edited on
Hi,

Your slotcolor function is doing 2 things: a function should only do 1 conceptual thing. You could use the array just like kemort said, but the switch should be another function IMO.

It's a good idea to make functions short, simple and hopefully reusable.

Good Luck !!
closed account (48T7M4Gy)
Actually TheIdeasMan taking your comment one step further the position array could be strings instead of ints with the written colors 'arrayed' - no ifs, no switch, just return the color as a string. A one/two liner single function.
Last edited on
Forgive my ignorance, but how is the wheel laid out again?

0 is green, then alternating red and black ? If so, why can't you use a simple modulo statement?

1
2
3
4
5
6
7
if (num == 0) {
   return 2; // green
}
else {
   color = num % 2;  // red is 1 or black is 0
   return color;
}


Is there a reason why you have split the wheel into quarters? Maybe there something else in the assignment that you haven't told us about?

Edit:

Didn't see your post kemort, if what I imagine is happening, then my code could be changed to return a string :+) between the pair of us we might get this fixed :+)
Last edited on
closed account (48T7M4Gy)
Along those lines,
http://www.roulette.com.au/strategy/playing-red-or-black/

It's getting easier all the time.

Maybe there something else in the assignment that you haven't told us about?
It doesn't really matter now that the ways and means are so much simpler than the OP system.
Topic archived. No new replies allowed.