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.
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.
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.
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.
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.
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 :+)