Hey everyone

just to refresh my memory and practice some programming I decided to make the dice program

here is my code:

#include <iostream>
#include <ctime>

using namespace std;

int main()
{
    int input;
    int random;

    cout << "             VIRTUAL DICE 1.0" << endl;

    cout << "Enter 1 to roll and 0 to quit: ";
    cin >> input;

    switch(random)
    {
    srand((unsigned)time(0));
    for(int i = 0; i <= 6; i++)
    {
        random = rand();
        cout << random << endl;
    }

    case 1:

    cout << "--------" << endl;
    cout << "|        |" << endl;
    cout << "|    O   |" << endl;
    cout << "|        |" << endl;
    cout << "--------" << endl;

    case 2:

    cout << "--------" << endl;
    cout << "|O       |" << endl;
    cout << "|        |" << endl;
    cout << "|       O|" << endl;
    cout << "--------" << endl;

    case 3:

    cout << "--------" << endl;
    cout << "|       O|" << endl;
    cout << "|    O   |" << endl;
    cout << "|O       |" << endl;
    cout << "--------" << endl;

    case 4:

    cout << "--------" << endl;
    cout << "|O      O|" << endl;
    cout << "|        |" << endl;
    cout << "|O      O|" << endl;
    cout << "--------" << endl;

    case 5:

    cout << "--------" << endl;
    cout << "|O      O|" << endl;
    cout << "|    O   |" << endl;
    cout << "|O      O|" << endl;
    cout << "--------" << endl;

    case 6:

    cout << "--------" << endl;
    cout << "|O      O|" << endl;
    cout << "|O      O|" << endl;
    cout << "|O      O|" << endl;
    cout << "--------" << endl;

    }

    system("PAUSE");
    return 0;
}

now this doesnt really work the way I want it to, the user inputs the number 1 and it will randomly show one dice on the screen but it breaks. I am missin the part if the user enter 0, I will do it after I figure this out.... also is there like a much better way to do this instead of doing it like this cause I would like to learn that.

Quick fix

#include <iostream>
#include <ctime>
#include <cstdlib>

using namespace std;

int main()
{
    int input;
    int random;

    cout << "             VIRTUAL DICE 1.0" << endl;
    srand((unsigned)time(0));

    do
    {
        cout << "Enter 1 to roll and 0 to quit: ";
        cin >> input;

        if (input == 1)
        {
            // Generate a number 1 - 6
            random = (rand() % 6) + 1;

            switch(random)
            {
                case 1:
                    cout << " -------- " << endl;
                    cout << "|        |" << endl;
                    cout << "|    O   |" << endl;
                    cout << "|        |" << endl;
                    cout << " -------- " << endl;
                    break;
                case 2:
                    cout << " -------- " << endl;
                    cout << "|O       |" << endl;
                    cout << "|        |" << endl;
                    cout << "|       O|" << endl;
                    cout << " -------- " << endl;
                    break;
                case 3:
                    cout << " -------- " << endl;
                    cout << "|       O|" << endl;
                    cout << "|    O   |" << endl;
                    cout << "|O       |" << endl;
                    cout << " -------- " << endl;
                    break;
                case 4:
                    cout << " -------- " << endl;
                    cout << "|O      O|" << endl;
                    cout << "|        |" << endl;
                    cout << "|O      O|" << endl;
                    cout << " -------- " << endl;
                    break;
                case 5:
                    cout << " -------- " << endl;
                    cout << "|O      O|" << endl;
                    cout << "|    O   |" << endl;
                    cout << "|O      O|" << endl;
                    cout << " -------- " << endl;
                    break;
                default:
                    cout << " -------- " << endl;
                    cout << "|O      O|" << endl;
                    cout << "|O      O|" << endl;
                    cout << "|O      O|" << endl;
                    cout << " -------- " << endl;
            }
        }
    } 
    while (input != 0);

    return 0;
}

As for a better way of doing it, you could store how the numbers are represented in 2D arrays perhaps?
Like :

0 0 0
0 1 0
0 0 0 (for 1)

And you could use a single display function that prints a '-' if there is a 0 in the matrix and an 'o' if there is a 1.

thanks for the fix gonbe.

@theguitarist I am terrible with arrays and I never understood working with them, could you please elaborate

Read here

So, you intialize 2-dimensional arrays (3 X 3), each representing a number on the dice.
'1' would be represented like I showed above. Right?

Your display function would need to iterate across each element of the array corresponding to the desired number and print an 'o' if it is 1, '-' otherwise.

That would be more systematic.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.