# c++ dice game using random numbers

I'm fairly new to c++ programming. It would be helpful if I could get a feedback on a dice game I wrote. I would really appreciate some tips as well as your opinions.

This program will begin by asking the user to enter an amount to bet (from $1-$1000). Next, the user is asked to guess the total that will occur when two dice are rolled. The program then “rolls two dice” (using random numbers) and visually displays the result. Then, if the player’s guess was correct, the player wins 8 times their bet divided by the number of possible way to get that roll. If the player’s guess was wrong, they lose their bet times the number of possible ways to get their guess.



#include <iostream>
#include <cstdlib>
#include <time.h>
using namespace std;

//main program
int main()
{
int guess;//user guess for the value of the dice
float bet;//the bet placed by the user
float possibility;//number of combinations each number can come
int dice1, dice2;//the random value between 1-6 for each dice
int total;//the total number after the value of dice1 and dice2 is added
float earned_or_lost_money;//earned or lost money depending on whether you win/lose

srand(time(0));//random number seed

cout << endl;//blank line

cout << " What do you guess the result will be?(2-12): ";
cin >> guess;//user input for guess

cout << " Enter amount of money to bet ($1-$1000): ";
cin >> bet;//user input for bet

cout << endl;//blank line

dice1 = rand() % 6 + 1;//value of first dice(1-6)
dice2 = rand() % 6 + 1;//value of second dice(1-6)

total = dice1 + dice2;//value of dice1+dice2

switch (guess)// values for possible combinations for the total value of the dices added
{

case 2: possibility = 1; break;
case 3: possibility = 2; break;
case 4: possibility = 3; break;
case 5: possibility = 4; break;
case 6: possibility = 5; break;
case 7: possibility = 6; break;
case 8: possibility = 5; break;
case 9: possibility = 4; break;
case 10: possibility = 3; break;
case 11: possibility = 2; break;
case 12: possibility = 1; break;
}//switch

cout << " For your $" << bet << " bet, the roll is" << endl; cout << endl;//blank line if (dice1 == 1)//drawing if the value of dice1 is 1 { cout << "=============" << endl; cout << "| |" << endl; cout << "| 1 |" << endl; cout << "| |" << endl; cout << "=============" << endl; }//if else if (dice1 == 2)//drawing if the value of dice1 is 2 { cout << "=============" << endl; cout << "| 2 |" << endl; cout << "| |" << endl; cout << "| 2 |" << endl; cout << "=============" << endl; }//else if else if (dice1 == 3)//drawing if the value of dice1 is 3 { cout << "=============" << endl; cout << "| 3 |" << endl; cout << "| 3 |" << endl; cout << "| 3 |" << endl; cout << "=============" << endl; }//else if else if (dice1 == 4)//drawing if the value of dice1 is 4 { cout << "=============" << endl; cout << "| 4 4 |" << endl; cout << "| |" << endl; cout << "| 4 4 |" << endl; cout << "=============" << endl; }//else if else if (dice1 == 5)//drawing if the value of dice1 is 5 { cout << "=============" << endl; cout << "| 5 5 |" << endl; cout << "| 5 |" << endl; cout << "| 5 5 |" << endl; cout << "=============" << endl; }//else if else if (dice1 == 6)//drawing if the value of dice1 is 6 { cout << "=============" << endl; cout << "| 6 6 |" << endl; cout << "| 6 6 |" << endl; cout << "| 6 6 |" << endl; cout << "=============" << endl; }//else if cout << endl;//blank line if (dice2 == 1)//drawing if the value of dice2 is 1 { cout << "=============" << endl; cout << "| |" << endl; cout << "| 1 |" << endl; cout << "| |" << endl; cout << "=============" << endl; }//if else if (dice2 == 2)//drawing if the value of dice2 is 2 { cout << "=============" << endl; cout << "| 2 |" << endl; cout << "| |" << endl; cout << "| 2 |" << endl; cout << "=============" << endl; }//else if else if (dice2 == 3)//drawing if the value of dice2 is 3 { cout << "=============" << endl; cout << "| 3 |" << endl; cout << "| 3 |" << endl; cout << "| 3 |" << endl; cout << "=============" << endl; }//else if else if (dice2 == 4)//drawing if the value of dice2 is 4 { cout << "=============" << endl; cout << "| 4 4 |" << endl; cout << "| |" << endl; cout << "| 4 4 |" << endl; cout << "=============" << endl; }//else if else if (dice2 == 5)//drawing if the value of dice2 is 5 { cout << "=============" << endl; cout << "| 5 5 |" << endl; cout << "| 5 |" << endl; cout << "| 5 5 |" << endl; cout << "=============" << endl; }//else if else if (dice2 == 6)//drawing if the value of dice2 is 6 { cout << "=============" << endl; cout << "| 6 6 |" << endl; cout << "| 6 6 |" << endl; cout << "| 6 6 |" << endl; cout << "=============" << endl; }//else if cout << endl;//blank line cout << " For a total of " << total << endl; cout << endl;//blank line if (guess == total)//if the user guessed correctly { earned_or_lost_money = bet * 8 / possibility;//calculation for money earned cout << " You were correct... since " << guess << " can come up " << possibility << " ways," << endl; cout << "You win$" << bet << "*8/" << possibility << "= $" << earned_or_lost_money << endl; }//if else//if the user guessed wrong { earned_or_lost_money = possibility * bet;//calculation for lost money cout << " You were wrong... since " << guess << " can come up " << possibility << " ways," << endl; cout << " You lost " << possibility << "*$" << bet << "= \$" << earned_or_lost_money << " dollars!!!!!!" << endl;
cout << " Thanks for your donation :)" << endl;
}//else
return 0;
}//end


The 2 previous answers provide a lot of good points, here are a few more suggestions:

## Generating Random Numbers

As of C++11 there are better random number generators to use that the C standard library rand() function. This stackoverflow question provides the following:

#include <random>
#include <iostream>

int main() {
std::random_device rd;
std::mt19937 mt(rd());
std::uniform_real_distribution<double> dist(1.0, 10.0);

for (int i=0; i<16; ++i)
std::cout << dist(mt) << "\n";
}


This provides a better distribution than the C function rand().

## Representing Money in Software

Banks don't like to lose money due to floating point error, nor can they legally cheat the costumers so they don't use floating point numbers to represent money, they use 2 integer values, one for dollars and one for cents. This prevents floating point errors from affecting the cash values involved.

## Complexity

Any function larger than one screen in most IDEs or editors is to large, it is very hard to write, read, debug and maintain functions this large. You can create a class as one of the other answers suggested, you can also just create a number of functions called from main(). If you continue learning software you will learn principles on why this is important.

• Two integers? Too wasteful and cumbersome. Better a single integer with twice the bits. Nov 10, 2020 at 20:46
• Or for JPY just the yen - or more likely some multi didgit library or if old enough BCD rather than binary integers Nov 11, 2020 at 10:37

For a beginner, this is a nice attempt.

# Avoid "using namespace std"

Though this is trivial for a ten lines of code project, it immediately starts leading to issues when code size increases. Avoid it now. You can alternatively type std::cout or rather using std::cout, here the effect is localized to just cout. Check here for more info on why "using namespace std" is consider bad.

# Prefer double to floating-point values

Double has 2x precision than floating-point values, thereore, provides more accuracy when performing calculations. Double on most machines is 8bytes whereas float is 4bytes, but the precision gain is worth it.

# Make use of random header

rand requires a seed to generate its pseudorandom numbers, if this seed is the same at each run, it produces the same output. This leads to some sort of dependency on the seed. rand also uses srand making it impossible to use with other random engine

# Consider using a class

Your main function is cluttered with code here and there, move them to separate class, you could have a class that handles everything concerning the dice and the rest other functionalities.

# Prefer \n to endl

Whenever you need a newline but don't explicitly need to flush the buffer, use \n. endl adds a newline and flushes the buffer, this would result to a slight drop in performance.

• Actually for accuracy reasons backs use 2 integers for money values, one for dollars and one for cents. This prevents floating point errors. Nov 10, 2020 at 17:54
• @pacmaninbw I totally agree. Nov 10, 2020 at 18:19
• Integers for money, sure. But why break it down into two, when handling just one is more efficient and convenient? And if it has twice the bits, you can store even bigger totals. Nov 10, 2020 at 20:48

Depending of what you want, there is some things you can do:

• refactor dice drawing function as there is a lot of redounding code
• a check for all inputs (nothing prevent your user to input bet out of 2-12 and money out of 0-1000, even negative bet or non numeric ones
• add a loop allowing player to play multiples games (with money carried between)
• i probably would have used a table to store possibilities of bet

Without further informations about what you want to improve, it's hard to be more specific