Putting using namespace std
at the top of every program is a bad habit a bad habit that you'd do well to avoid. I don't know that you've actually done that, but it's an alarmingly common thing for new C++ programmers to do.
Putting using namespace std
at the top of every program is a bad habit that you'd do well to avoid. I don't know that you've actually done that, but it's an alarmingly common thing for new C++ programmers to do.
Putting using namespace std
at the top of every program is a bad habit that you'd do well to avoid. I don't know that you've actually done that, but it's an alarmingly common thing for new C++ programmers to do.
An alternative could be to do the following, preferably within the calling function.
using std::cout;
You are currently using this code to generate random numbers:
You are currently using
An alternative could be to do the following, preferably within the calling function.
using std::cout;
You are currently using this code to generate random numbers:
I have found a couple of things that could help you improve your code.
Don't abuse using namespace std
Putting using namespace std
at the top of every program is a bad habit that you'd do well to avoid. I don't know that you've actually done that, but it's an alarmingly common thing for new C++ programmers to do.
Separate I/O from game logic
The program currently has game logic and I/O all intermixed. It would be better to separate the two. Specifically I'd recommend that the player, each bet and the roulette wheel each be made separate objects. That way, if you decided to have multiple players with multiple types of bets at the same wheel, it could be done very simply by applying the state of the wheel to each bet and then updating the player's status (account balance) accordingly.
Eliminate "magic numbers"
There are a few numbers in the code, such as 1
and 36
that have a specific meaning in their particular context. By using named constants such as MINIMUM_BET_AMOUNT
or WHEEL_SLOTS
, the program becomes easier to read and maintain. For cases in which the constant only has sense with respect to a particular object, consider making that constant part of the object.
Reduce the scope of variables
Within the current code essentially all variables are all in the same scope. Better would be to minimize the scope of variables so that a variable such as betAmount
are only in scope for the duration they're actually needed. Object orientation will help a great deal with that.
Add realism to the game
Most real roulette tables have at least one zero slot (many have both 0 and 00). Adding those to the game would make things a little more realistic. I've also seen video screens in casinos which display the last dozen or so results. On a fair wheel this, of course, has no useful value to bettors, but people often have fun looking at the previous results and making some bet based on that, thereby illustrating why they call it the Gambler's fallacy.
Use a menu object or at least a common menu function
In a number of places in your code, you have something like a menu. Your code presents a couple of options and then asks the user to pick one based on an input number. Rather than repeating that code in many places, it would make sense to make it generic. Only the prompt strings actually change, but the underlying logic of presenting the choices and asking for input are all the same. It looks like you're a beginning programmer, and so perhaps you haven't learned about objects yet, but this kind of repeated task with associated data is really well-suited to object-oriented programming and that's something that C++ is very good at expressing.
Consider using a better random number generator
You are currently using
srand(time(0));
randomNumber = 1 + (rand() % 36);
There are two problems with this approach. One is that the low order bits of the random number generator are not particularly random, so neither with random1
be. On my machine, there's a slight but measurable bias toward 0 with that. The second problem is that you should only seed the random number generator once and not every time it's used.
A better solution, if your compiler and library supports it, would be to use the C++11 `std::uniform_int_distribution. It looks complex, but it's actually pretty easy to use:
// random number generator from Stroustrup:
// http://www.stroustrup.com/C++11FAQ.html#std-random
int rand_int(int low, int high)
{
static std::default_random_engine re {};
using Dist = std::uniform_int_distribution<int>;
static Dist uid {};
return uid(re, Dist::param_type{low,high});
}
For a roulette table with possible values 0 to 36, call it with rand_int(0,36);
.