Dice Game

Hello! I'm trying to write a program that will generate a value between one and six. (Simulating a dice roll) Each of these values will output a different message and add said value to the variable "num". The user will be prompted to press 'y' to loop the program again. However, once num reaches a value => 30, the program will end. The only trouble I'm having is that it generates the same value/message every time you loop the program. The initial value/message is randomized every time you run the program, but the loop just repeats the same value/message until you reach a number => 30. I want the dice roll to be randomized every time you loop the program instead of every time you run the program.

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

using namespace std;

int main()
{
    
    int number = 6;
    int result = 0;
    int num = 0;
    char choice = '\0';
    
    srand(time(NULL));
    
    result = rand() % number + 1;
    do
    {

    switch(result)
    {
        case 1:
            num = num + 1;
            cout << "You rolled a one!" << endl;
            cout << "Num = " << num << endl;
            break;
        case 2:
            num = num + 2;
            cout << "You rolled a two!" << endl;
            cout << "Num = " << num << endl;
            break;
        case 3:
            num = num + 3;
            cout << "You rolled a three!" << endl;
            cout << "Num = " << num << endl;
            break;
        case 4:
            num = num + 4;
            cout << "You rolled a four!" << endl;
            cout << "Num = " << num << endl;
            break;
        case 5:
            num = num + 5;
            cout << "You rolled a five!" << endl;
            cout << "Num = " << num << endl;
            break;
        case 6:
            num = num + 6;
            cout << "You rolled a six!" << endl;
            cout << "Num = " << num << endl;
            break;
    }
        
        if (num < 30)
        {
        cout << "Do you want to roll again? " << endl;
        cin >> choice;
        }
        else
        {
            cout << endl;
            cout << "You win!" << endl;
            cout << endl;
        }

    }
    while (choice == 'y' && num < 30);
    
    

    return 0;
}


Help would be greatly appreciated. Thank you!
Move line 18 to line 21 (inside the loop).
You're calling rand() only once (before you enter the loop).
Topic archived. No new replies allowed.