Help with Dice program in c++ pleaseeeee

I need help with writing an interactive program that simulates the roll of two die. It must use functions and output something that looks like dice with the dots as numbers on them. Also make a function to output the dice and allow the user to roll until they choose to quit. and i need them to print side by side... please im new to this and I'm struggling with imputing the second die...


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
 //Library files
#include <iostream>
#include<conio.h>
#include<time.h>
#include<stdlib.h>

using namespace std;
int roll()
{
    static int const max = RAND_MAX/6*6;

    int r = rand();
    while(r >= max)
    {
        r = rand();
    }

    return r%6+1;
}

void call();
void one();
void two();
void three();
void four();
void five();
void six();
void call();

int main()
{

    cout<<"\n\n\t\tPress r to roll or q to quit the game ";
    char ch;
    ch = getch();
xm:
    if (ch=='r')
    {
        system("cls");
        call();
    }
    else
        exit (0);
    cout<<"\n\n\t\tPress r to roll again q to quit: ";
    ch = getch();
    goto xm;
    getch();

}

void call()
{
    srand (time(NULL));

    int n;
    n= rand();
    n = 1 + n % 6;

    switch (n)
    {
    case 1:
        one();
        break;
    case 2:
        two();
        break;
    case 3:
        three();
        break;
    case 4:
        four();
        break;
    case 5:
        five();
        break;
    case 6:
        six();
        break;

    }
}

void one()
{
    cout << " -----\n";
    cout << "|     |\n";
    cout << "|  O  |\n";
    cout << "|     |\n";
    cout <<  " -----\n";
}
void two()
{
    cout << " -----\n";
    cout << "|    O|\n";
    cout << "|     |\n";
    cout << "|O    |\n";
    cout <<  " -----" << endl;
}
void three()
{
    cout << " -----\n" ;
    cout << "|    O|\n";
    cout << "|  O  |\n";
    cout << "|O    |\n";
    cout <<  " -----" << endl;
}
void four()
{
    cout << " -----\n" ;
    cout << "|O   O|\n";
    cout << "|     |\n";
    cout << "|O   O|\n";
    cout <<  " -----" << endl;
}
void five()
{
    cout << " -----\n";
    cout << "|O   O|\n";
    cout << "|  O  |\n";
    cout << "|O   O|\n";
    cout <<  " -----\n";
}
void six()
{
    cout << " -----" << endl;
    cout << "|O   O|" << endl;
    cout << "|O   O|" << endl;
    cout << "|O   O|" << endl;
    cout <<  " -----" << endl;
}
All your roll() function needs to return is : return (rand() % 6) + 1;

Instead of using goto (which is evil!) why not:
1
2
3
4
5
(while ch != 'q')
{
  // process rolls...
  // prompt again for r or q
}


As for getting the dice to display side by side, you need to rethink your output methods. Think about storing each "line" of ouput (top to bottom) in an array, to make them easier to line up horizontally like perhaps:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
const char *one[] = { " ----- ", "|     |", "|  o  |", "|     |", " ----- " };
const char **getDie(int i)
{
  switch(i)
  {
    case 1: return one;
    ...
  }
}
void display(int diceRoll1, int diceRoll2)
{
  for(int idx = 0; idx < 5; ++idx)
  {
    std::cout << (getDie(diceRoll1))[idx] << "  " << (getDie(diceRoll2))[idx] << std::endl;
  }
}
Topic archived. No new replies allowed.