HiveBrain v1.2.0
Get Started
← Back to all entries
patterncppMinor

Text-based dice rolling game in C++

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
textrollinggamedicebased

Problem

I have made this small text-based dice rolling game in C++. How could I improve it? Be as picky as you'd like.

#include 
#include 
#include 

using namespace std;

void mainGame(int &numDice);

int main()
{   
    srand (time(NULL));

    int gameRunning = true;
    int numDice;

    while (gameRunning == true)
    {
        system("cls");
        cout  ";
        cin >> numDice;

        if (numDice == 1 || numDice == 2 || numDice == 3 || numDice == 4) 
        {
            mainGame(numDice);
        }

        else if (numDice == 0)
        {
            return 0;
        }

        else {
            system("cls");
            cout  ";
        cin >> input;
        switch (input)
        {
            case 1:
                break;

            case 2:
                goAgain = false;
        }
    }
}

Solution

using namespace std; is considered a bad practice.

Don't do this:

int gameRunning = true;


If you want to use a boolean variable, use a bool instead of int.

You can use boolean values directly in conditions,
no need to compare them to true / false.
So instead of this:

while (gameRunning == true)


You can write simply:

while (gameRunning)


Still about that while loop earlier,
since gameRunning is initialized to true,
the loop condition will be inevitable true for the first time.
That's kind of a waste to re-evaluate.
In situations like this,
considering rewriting it as a do { ... } while (...) loop.

Code Snippets

int gameRunning = true;
while (gameRunning == true)
while (gameRunning)

Context

StackExchange Code Review Q#93458, answer score: 6

Revisions (0)

No revisions yet.