Defining chars as more than one option (Uppercase vs Lowervase)

Hey programmers, I'm new to C++ and I'm trying to make a basic roulette program. In the instructions, we're supposed to take bets in the form of single letters, but uppercase AND lowercase need to be acceptable. Here's one part where I define these:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
if ((spinnum >= 1 && spinnum <=10) || (spinnum >= 19 && spinnum <= 28))
	{
		if (spinnum % 2 == 0)
		{
			wheelcolor = 'B' | 'b';
			EoO = 'E' | 'e';
		}

		else if (spinnum % 2 == 1)
		{
			wheelcolor = 'R' | 'r';
			EoO = 'O' | 'o';
		}
	}


But whenever I run the program and it runs a loop like the one below, in the debug line wheelcolor only prints the lowercase letter for whatever the color would be. This is fine, but the program is only correct when i run it if i input a lowercase letter. ex. if I bet B and the number ends up being black the debug will print "b" and it'd say that I lost.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
	//DEBUG
	cout << endl << spinnum << " " << wheelcolor << endl;
	//DEBUG

	if (inichoice == 'R' || inichoice == 'r') 
		{
			if (inichoice == wheelcolor)
			{
				cout << "The wheel is spinning...and landed on " << wheelcolor << "." << endl;
				cout << "We have a winner in the house!";
			}
			else
			{
				cout << "The wheel is spinning...and landed on " << wheelcolor << "." << endl;
				cout << "You tried your best, loser.";
			}
		}


NOTE: This isn't the whole code, but these sections of code are where I'm having the issues.
Thank you very much for any help!
Last edited on
we're supposed to take bets in the form of single letters, but uppercase AND lowercase need to be acceptable.

I don't see anything in your code having to do with taking bets.

First snippet:
Line 5,6,11,12: You're doing a bitwise or of two constants. The result of that is going to be a lower case character. You could have just as well have done:
 
  wheelcolor = 'r';  

It's not clear what you're trying to do here. You may want to look at toupper() or tolower().
http://www.cplusplus.com/reference/cctype/toupper/
http://www.cplusplus.com/reference/cctype/tolower/

Line 9: There is absolutely no reason for the second if. If remainder of a modulo 2 operation is not 0, the only other possibility is that it is 1.
sorry if I wan unclear, I'll just stick the whole program in here because that'll probably help a lot more:
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <ctime>
#include <cmath>
#include <string>


using namespace std;
int main()
{
	

char inichoice, color, EoO, playagain;
char wheelcolor;
int spinnum = 0, num = 0;

do //cause it's gotta loop later if they wish for so
	{
	
	cout << "What type of bet you want yo?\n (S = Single number, R = Red, B = Black, E = Even, O = Odd) ";
	cin >> inichoice;
	//start of program where initial choice is made
	
	srand(time(0));
	spinnum = rand( ) % 37;
	
	//DEBUG
	//spinnum = 13;
	//DEBUG
	
	//defining the lucky number! #gamblingaddiction #whywegottadodisman #takemymoneyandkeepit
	
	if ((spinnum >= 1 && spinnum <=10) || (spinnum >= 19 && spinnum <= 28))
		{
			if (spinnum % 2 == 0)
			{
				wheelcolor = 'B' | 'b';
				EoO = 'E' | 'e';
			}

			else if (spinnum % 2 == 1)
			{
				wheelcolor = 'R' | 'r';
				EoO = 'O' | 'o';
			}
		}
// this if statement helps determine the color and the nature (even or odd) of the number
		
	if (spinnum == 0)
		wheelcolor = 'G';
	// 0 is the exception in roulette and that's defined here
		
	if ((spinnum >= 11 && spinnum<=18) || (spinnum >= 29 && spinnum <= 36))
		{
			if (spinnum % 2 == 0)
			{
				wheelcolor = 'R' | 'r';
				EoO = 'E'| 'e';
			}
			
			else if (spinnum % 2 == 1)
			{
				wheelcolor = 'B' | 'b';
				EoO = 'O' | 'o';
			}
		}
	//this is the last of the statements used to define color and nature
		//needed so many because roulette is weird and the colors switch around



	//DEBUG
	cout << endl << spinnum << " " << wheelcolor << endl;
	//DEBUG

	if (inichoice == 'R' || inichoice == 'r') 
		{
			if (inichoice == wheelcolor)
			{
				cout << "The wheel is spinning...and landed on " << wheelcolor << "." << endl;
				cout << "We have a winner in the house!";
			}
			else
			{
				cout << "The wheel is spinning...and landed on " << wheelcolor << "." << endl;
				cout << "You tried your best, loser.";
			}
		}
	else if (inichoice == 'B' || inichoice == 'b')
		{
			if (inichoice == wheelcolor)
			{
				cout << "The wheel is spinning...and landed on " << wheelcolor << "." << endl;
				cout << "Well would you gosh diddly darn look at this! You won!";
			}
			else
			{
				cout << "The wheel is spinning...and landed on " << wheelcolor << "." << endl;
				cout << "Haha, you suck at this!";
			}
		}
			
//This if statement is the block of code that runs if the player picks a color
	
	else if (inichoice == 'S' || inichoice ==  's')
		{
			cout << "Enter a number between 0 and 36: ";
			cin >> num;
				if (spinnum == num)
				{
					cout << "The wheel is spinning...and landed on " << spinnum << endl;
					cout << "Winner winner chicken dinner!";
				}
				else
				{
					cout << "The wheel is spinning.. and landed on " << spinnum << endl;
					cout << "You are the champi--LOSER, my friieenndd~";
				}
		}
//This block of code plays when the player enters a single number
	
	else if (inichoice == 'E' || inichoice == 'e')
		{
			if (spinnum % 2 == 0)
				{
					cout << "The wheel is spinning...and landed on " << spinnum << endl;
					cout << "Lucky ducky! You win!";
				}
			else
				{
					cout << "The wheel is spinning...and landed on " << spinnum << endl;
					cout << "Haha. Wow. You lost...Stop wasting your time here!";
				}
		}
//This block of code plays when they bet on even numbers
	else if (inichoice == 'O' || inichoice == 'o')
		{
			if (spinnum % 2 == 1)
				{
				cout << "The wheel is spnning...and landed on " << spinnum << endl;
                cout << "Congratulations, you won! Wow! I knew you had it in you!" << endl;
				}
			else
				{
				cout << "Spin, spin, spin the wheel along... " << spinnum << endl;
                cout << "How you managed to lose is beyond me, but I bet this is why you can't have nice things..." << endl;	
				}
		}
//this block of code plays when they bet on odd numbers

// all this crazy stuff above is for each different type of bet and what happens after the user places it
	
	else
		cout << "This isn't valid. Stop making this longer than it has to be :(";
//here's the statement that makes the user regret living when they enter something wrong

cout << "\n\nWanna play again? (Y for yes) ";
cin >> playagain;
cout << endl;
	}
while (playagain == 'y' || playagain == 'Y');
//these lines above restart the loop if the play wants to go again

cout << "\nFINALLY, leave me alone already!";	
	
	return 0;
}
Line 25: Do not call srand() multiple times (within a loop). srand() sets the RNG to a particular starting point. Calling srand() repeatedly can cause the RNG to return the same random numbers. srand() should be called ONCE at the beginning of main().
http://www.cplusplus.com/reference/cstdlib/srand/

Line 38,39,44,45,58,59,64,65: Still have the same problem I pointed out before. Why are you doing a bitwise or ?

Line 42,62: The if is still unnecessary.

I fixed the extra if statements for the modular division, thanks for that!
For the RNG seeding I think I get what you mean, but if it's only run once, wouldn't it always use the same value as the seed each time the loop is executed?

For the issues in lines 38, 44 and so on.. this is where a majority of my confusion comes from. I'm still very new to this, I don't even know what bitwise means.. I've been looking around for information on this and I THINK we're supposed to use a string for those lines, but we never really learned how to set them up to be more than one letter as opposed to a straight up sentence (is string A (Even Odd) the same as making the string equal "Even" and "Odd"?). Beyond that, we definitely didn't cover how to put strings into if statements, or how to use strings in ANY sort of decision statement..

thanks again for the help on this!
f it's only run once, wouldn't it always use the same value as the seed each time the loop is executed?

No.

I don't even know what bitwise means.

See the BITWISE OR section here:
http://www.cprogramming.com/tutorial/bitwise_operators.html

Using a char for wheelcolor is fine, you just have to standardize on whether the default in you program is upper or lower case. Assuming upper case, line 38 becomes:
1
2
 
  wheelcolor = 'B';


Now at line 79, you have a problem. inichoice can be upper or lower case. However, you can fix that as I previously pointed out using toupper().
77
78
79
  inichoice = toupper(inichoice);  // Convert whatever the user entered to upper case
  if (inichoice == 'R')   // We only need to test for upper case
  {  if (inichoice == wheelcolor)  // This this test now works because wheelcolor is upper case 



OH gotcha, that was actually a pretty easy fix then! thank you so much!
Its funny how people all over the world start with similar things haha. Im working on a roulette program aswell (Next big casino guru here:) and this helped me out alot as i had a similar problem even if it was not to do with uppercase [url=http://köpersterbhusstockholm.se/koper-saljer-fastigheter/]letter[/url]. Cheers guys. Looking forward to learn more!
Last edited on
Topic archived. No new replies allowed.