This is pretty good code for a first draft. Everything you've written is easy to understand. I didn't have to guess what anything was doing, which is awesome!
Most of my suggestions are nitpicky and may seem a bit grandiose for a small program like this, but it's worth getting into the habit of doing these things because programs often grow quickly and it can be hard to manage that change when they do.
Use The Right Function
rand()
is a terrible random generator. The man
page for rand()
describes it this way:
RAND(3) BSD Library Functions Manual RAND(3)
NAME
rand, rand_r, srand, sranddev -- bad random number generator
You should try something like arc4random()
or some other better random number generator.
Group Functionality Into Functions
I would make this entire block a separate function named something like displayInstructions()
:
system("cls");
cout << "\n How many dice would you like to use? (1-4)" << endl;
cout << "\n Enter '0' to exit." << endl;
cout << "\n> ";
Likewise with the error text. I'd put it into a function named something like displayError()
:
system("cls");
cout << "\n That was invalid input!" << endl;
cout << "\n ";
system("pause");
It's likely that in the future there may be more than 1 type of error. (In fact there are already 2 places where the input can be invalid - when getting the number of dice and when deciding to run again or not.)
The mainGame()
function actually does 2 different things. It displays the dice rolls, and then it determines if the user wants to roll again. I would split out the second part into a function named something like rollAgain()
. And to make it simpler, I'd probably remove the case
statement and just use a couple of if
s. And if you want to handle errors, you'll need to have a loop in there:
bool rollAgain()
{
for (;;)
{
system("cls");
cout << "\n Would you like to go again with " << numDice << " dice?" << endl;
cout << "\n Enter '1' for yes or '2' for no." << endl;
cout << "\n> ";
cin >> input;
// If the input is 1 or 2 it's valid, so return true if it's 1, false otherwise
if ((input == 1) or (input == 2))
{
return input == 1;
}
// The input was not valid, display an error and ask for input again
displayError();
}
}
Naming Is The Hardest Part
It's said that the hardest part of computer programming is naming things. The name mainGame()
doesn't say a whole lot. I'd name it something more descriptive like rollDice()
. Because eventually, you'll probably want to use the results of those dice rolls to do something more interesting than just display them to the user.
If You Aren't Going To Change It, Make It Const
In mainGame()
, the numDice
argument isn't changed. It should be const int numDice
in that case.
Magic Numbers
And speaking of constants, I notice that you have several numbers hard-coded in your code. Generally, it's a good idea to make those into named constants so you or other people can remember what they represent. So instead of using 1
and 4
in your if
statement, I'd make 2 constants like this:
const int MIN_NUM_DICE = 1;
const int MAX_NUM_DICE = 4;
and the if
would look like this:
if ((MIN_NUM_DICE <= numDice) and (numDice <= MAX_NUM_DICE))
{
mainGame(numDice);
}
This way, if you ever decide you want more dice rolls you change the constant in one place, and if it's a value that's used in multiple places you don't have to change it everywhere it's used.
I'd also change the 6
in mainGame()
to a constant. Something like:
const int NUM_SIDES_PER_DIE = 6;
Maybe someday you'll want to support 12-sided or 20-sided die, for example. If you do, then it's a lot easier to understand that the rand
call is you "dice roll":
dice[x] = (rand() % NUM_SIDES_PER_DIE) + 1;
User Interface
And if I'm being really nit-picky, instead of using 1
and 2
to choose whether or not to play again, make it a yes/no question and let the user enter y
(or Y
) or n
(or N
).
improve
, are you looking for performance or readability? \$\endgroup\$numDice == 1 || numDice == 2 || numDice == 3 || numDice == 4
tonumDice >= 1 && numDice <= 4
. This will also technically speed it up a minuscule amount ofnumDice
is 0, 3, 4, 5 or 6. That's a pretty quick and easy one. \$\endgroup\$cout
lines being a bit long, but that's not necessarily your fault.) \$\endgroup\$