Need help on Dice Game!

Rather than flame me, please give me constructive criticism as Im very new to programming and still in the learning phase.

My objective: make a dice game where the user can choose to continue or quit, and if he chooses to continue then he would be asked how many times he wants the computer to roll a dice (he can only choose 1-6 because anything after 6 would mess with my switch statement). Say you type 3, the computer rolls 3 random numbers from 1 to 6 (which is a dice) and prints the output. You can continue to type a number for the computer to roll or you can type -1 to finish. If you type -1, the computer adds up all of the times 1, 2, 3, 4, 5, and 6 were rolled and puts an output at the end for the result.

My problem: I cant get the result to output correctly.. It is taking the value of what the user inputs for how many times they want to roll. I know thats whats going wrong, but I dont know how to rewrite the code to do specifically what Im wanting it to do (which I stated above). Can anyone help please? Thanks!

Hope Ive explained thoroughly.

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
  #include <iostream>
#include <string>
#include <cmath>
#include <cstdlib>
#include <ctime>

using namespace std;

int main()
{
    string rollorquit;
    int one = 0, two = 0, three = 0, four = 0, five = 0, six = 0;
    int wanttoroll;

    cout << "------------------------------------DICE GAME-----------------------------------\n";

    cout << "Type 'roll' to roll, or 'quit' to quit\n";
    cin >> rollorquit;
    cin.ignore();

    if (rollorquit != "roll"){
        cout << "PROGRAM TERMINATED";
        }

    while (rollorquit == "roll" && wanttoroll != -1){

        while (wanttoroll != -1){
        cout << "How many times do you want to roll?\n(cant be more than 6) or type -1 to finish: ";
        cin >> wanttoroll;
        cin.ignore();

        srand(time(0));

        for (int x = 0; x < wanttoroll; x++){
            cout << 1+(rand()%6) << endl;

    switch (wanttoroll){
    case 1:
        one++;
        break;
    case 2:
        two++;
        break;
    case 3:
        three++;
        break;
    case 4:
        four++;
        break;
    case 5:
        five++;
        break;
    case 6:
        six++;
        break;
    default:
        cout << "The program had an error!";
                }
            }
        }
        cout << "The total times the number one came up: " << one << endl;
        cout << "The total times the number two came up: " << two << endl;
        cout << "The total times the number three came up: " << three << endl;
        cout << "The total times the number four came up: " << four << endl;
        cout << "The total times the number five came up: " << five << endl;
        cout << "The total times the number six came up: " << six << endl;
    }
    return 0;
}
You want to store the result of the random number generator on line 35 and use that for your switch instead. wanttoroll is your variable for the number of rolls, not the result of a roll.
Last edited on
I changed the variable in the switch statement to equal randvar, and set randvar equal to the result of random number generator. But when I compiled, it was spitting out random numbers. It said one was randomly rolled 3 times, but it was only twice. Two randomly rolled 4 times, but it was rolled once. You get the idea.. what am I doing wrong? Give me an example please?

 
randvar = 1+(rand()%6);
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
while (rollorquit == "roll" && wanttoroll != -1){

	while (wanttoroll != -1){
		cout << "How many times do you want to roll?\n(cant be more than 6) or type -1 to finish: ";
		cin >> wanttoroll;
		cin.ignore();

		//srand(time(0));

		for (int x = 0; x < wanttoroll; x++){
			int rvar = 1+(rand()%6);
			cout<<rvar<<" "<<endl;
	
			switch (rvar){
				case 1:
					one++;
					break;
				case 2:
					two++;
					break;
				case 3:
					three++;
					break;
				case 4:
					four++;
					break;
				case 5:
					five++;
					break;
				case 6:
					six++;
					break;
				default:
					cout << "The program had an error!";
			}
		}
	}
	cout << "The total times the number one came up: " << one << endl;
	cout << "The total times the number two came up: " << two << endl;
	cout << "The total times the number three came up: " << three << endl;
	cout << "The total times the number four came up: " << four << endl;
	cout << "The total times the number five came up: " << five << endl;
	cout << "The total times the number six came up: " << six << endl;
}


It works fine for me and I didn't really change your code. I just changed the formatting. I commented out srand for consistency while testing. Are you sure the count for the frequency is random? Are you printing the result of each roll and checking against the frequency at the end?
Last edited on
Im not sure I understand your question. What Im doing is seeing how many times each number shows up in all of the rolls. If he enters 3, 5, 4, then -1 to finish, the program prints 3 random numbers, 5 random numbers, then 4 random numbers. Then it would check to see how many tmies 1, 2, 3, 4, 5, 6, were printed out, if at all, and give us the output.
Nevermind! I found the solution =D!!!! I did what you said except I forgot to remove the
cout << 1+(rand()%6) << endl;

from my for loop and replace it with the new variable equaling the random number generator, then outputting that new variable and adding it into the switch statement. THANK YOU
BTW, why not using array like this

 
int points[6];


instead of these clumsy "one", "two", ..., "six"?

You can then use:

 
points[rand() % 6]++;


instead of this great switch. And also use loop with 6 iterations instead of 6 lines of printing in the end.
Dont forget: this is my first program. I know what an array is but dont really know how to implement them, and same for loops, I still have a hard time implementing some concepts. But it should get better over time.
Topic archived. No new replies allowed.