Roulette game

Hi,

im new to this forum and been trying learn C++ on my own

now as a fun project i wanted to create a Roulette game
but now i came across my first problem

would really appreciate your help but please do not simply give me the correct code as i want to understand how and why

this is where my problem comes


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
  int black[18] = {2,4,6,8,10,11,13,15,17,20,22,24,26,28,29,31,33,35 };
int red [18] = {1,3,5,7,9,12,14,16,18,19,21,23,25,27,30,32,34,36 };
int green = 0;
string a = " Black ";
string b = " Red ";
string c = " Green ";

int playRoll(){
int result = 0+(rand() % 36);
if(result == black){color = a}
else if(result == red){color = b}
else if (result == green){color = c}
int color;
    cout << setw(20) << " Spinning the wheel " << endl;
    cout << endl;
    cout << " The Winning number is " << color << result << endl;

}


upon compilling i get this error

*error ISO C++ Forbids comparison between pointer and integer [-fpermissive]

i dont understand this error
i dont know what piece of code is reffereed to as the pointer

also if you can give suggestions on how i can do "what im trying to do" in a better way

Thank you
Last edited on
result == black

What kind of object is result? An int.
What kind of object is black? An array of int (which will be decayed in usage to a pointer to the first int in the array)

Does it make any sense to compare these two objects? No.
Last edited on
Ok so am i Able to acces the array to get a black result or do I need to approach this different
Couple of tips:

1) Give variable names more meaning. "a" isn't a good variable name.
2) Every time you do something that computer could do(and is tiresome), try to think if you can improve anything. For example your "black" and "red" array are not only a bit arbitrary(some comments could help), they're created by hand. You could implement a function like:

1
2
3
4
5
6
7
8
9
//Vector is essentially a smarter array. & means that you pass a reference
//Reference is something like a pointer, but simpler. You can think of this
//operation as "pass the destination, instead of grabbing a copy to it
void AddEvenRange(int begin, int end, std::vector<int>& destination)
{
    if(begin % 2 == 1) ++begin;
    for(; begin <= end; begin += 2)
        destination.push_back(begin);
}


And so you could use this, along with other utility AddOddRange, to generate black and red(a bit like this):

1
2
3
4
5
std::vector<int> black;
AddEvenRange(2,10,black);
AddOddRange(11,17,black);
AddEvenRange(20,28,black);
AddOddRange(29,35,black);


Or better yet, put this in another utility, so the code is clearer:

1
2
3
4
5
6
7
void InitializeBlack(std::vector<int>& black)
{
    AddEvenRange(2,10,black);
    AddOddRange(11,17,black);
    AddEvenRange(20,28,black);
    AddOddRange(29,35,black);
}


This project might be too small for this, but this would make the code easier to read anyway.

Next, 0+ X = X, so you don't need to do that at line 9. Also, since it's C++, use std::mt19937 from <random> instead of rand() (homework: google "rand considered harmful).

As for error, it's simple: array is a pointer too. You're comparing int*(or int[]) with int. This won't do. Instead, you should check whether result is in the array. Using vector, you could do:

1
2
3
std::vector<int> black;
/*...*/
if(std::find(black.begin(), black.end(), result) != black.end())//... 


Generally, when writing the code, try to think about the higher level. You're not "checking if color is in the array", you're "checking if color is black". The code is often easier to read if it's composed of small utilities that make it up. The intellectual effort to understand the program as a whole might be a little higher, but at the same time the program is easier to read, since it encapsulates abstractions and uses natural language to describe the problem, leaving implementation details out of the way.

Cheers!

PS. Oh, and please run the code in your head before running it on the PC. You are using a variable called color before creating it. That's an error.

PPS. I also noticed that the arrays(vectors) are storted. The find(if I remember things correctly) uses linear search algorithm, which works in O(n) time. Since you are sure that your arrays are sorted, you can use binary search algorithm( http://www.cplusplus.com/reference/algorithm/binary_search/ ), which is much faster(it's O(log2(n))). It might not be much for this short sequence, but it's good to remember anyway.
Last edited on
Wow thanks for the datailed reply
Sadly I don't understand much of it yet
Guess I got some reading up to do
Thanks for helping me out

Cheers
Topic archived. No new replies allowed.