Problem using random function.

Hello, first of all I should say I do not get any compile errors, however the code is not working as I want it to.

I'm trying to create a program that generates numbers from 1-6 (a dice). The user should get to choose between high( x > 3 ) or low ( x < 4 ). After the input he should get a result with informationn if his guess was right or not.

Example:

1
2
3
Throw the dice by typing either "h" (High) or "l" (Low) and then pressing enter: h

Congratulations, you picked "h" and the dice shows: 5


As far as I see, I've done something crazy at the void output(char choice)function because the program never prints out the choice in: (line 83)

 
cout << "Sorry, you lost. you picked \"" << choice << "\" and the dice shows: " << random << endl;


Would appriciate if someone could help me out here, been trying to solve this the past hour.

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

using namespace std;

int input(char dice);
int dicethrow(int dice, char choice);
void output(char choice);


int main()
{
    int random{}, throwit{};
    char dice;

    srand(time(NULL));

    input(dice);
    output(dice);
    dicethrow(random, dice);

    return 0;
}

int input(char dice)
{
    cout << "Throw the dice by typing either \"h\" (High) or \"l\" (Low) and then pressing enter: ";
    cin >> dice;

    if(dice == 'h' || dice == 'H')
    {
        return dice;
    }
    else if(dice == 'l' || dice == 'L')
    {
        return dice;
    }
    else
    {
        cout << "Invalid entry, try again." << endl;
        input(dice);
    }
}

int dicethrow(int dice, char choice)
{
    cout << "Would you like to throw it again? (y / n): ";
    cin >> choice;

    if(choice == 'y' || choice == 'Y')
    {
        input(dice);
        output(choice);
        dicethrow(dice, choice);
    }
    else if(choice == 'n' || choice == 'N')
    {
        return 0;
    }
    else
    {
        cout << "Invalid entry, try again." << endl;
        dicethrow(dice, choice);
    }
}

void output(char choice)
{
    int random{};
    random = (rand() % 6) + 1;

    if(random < 4 && (choice == 'l' || choice == 'L'))
    {
        cout << "Congratulations, you picked \"l\" and the dice shows: " << random;
    }
    else if(random > 3 && (choice == 'h' || choice == 'H'))
    {
        cout << "Congratulations, you picked \"h\" and the dice shows: " << random;
    }
    else
    {
        cout << "Sorry, you lost. you picked \"" << choice << "\" and the dice shows: " << random << endl;
    }
}
int line 15 you declare char dice . You then pass dice to output without assigning any value to it.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
int main()
{
    int random{}, throwit{};
    char dice;
   /*
    cout<<"Enter choice: ";
    cin>>dice;
    */
    srand(time(NULL));

    input(dice);
    output(dice);
    dicethrow(random, dice);

    return 0;
}
Variable scope. When you pass dice into output, dice actually is not initialized (i.e. is not storing the user's input like you want). This is because you're passing dice into input by value. This means when the user inputs their guess, it is stored locally in input and destroyed when the function returns.

Pass by reference instead which requires you to add an ampersand before the parameter name. Passing by reference allows you to modify the variable pass rather than just modifying a copied value.

void input (char &dice) { /*function stuff */ }

I also made your function void because it seems you'd do nothing with the return value.
Passing by reference allows you to modify the variable pass

I dont think that's what kemkoi wants.
That was actually exactly what I wanted. It's working as intended now but why would you change it to a void? I return dice twice in the function and as far I know I need to use something else than a void when I want to return something.

@shadowCODE

I declare char dice in the input function, I have no idea what you're talking about. I'm declaring the varible in the main function to use it for the function..
Last edited on
In this case, you actually don't need to return a value. Passing-by-reference often times eliminates the need to return a value. All you want to do is modify main's dice variable so that you can pass it into the output function. So if passing by reference accomplishes that, what do you actually need to return?

The "Arguments passed by value and by reference" section on this tutorial page does a good job at explaining it better than I probably: http://www.cplusplus.com/doc/tutorial/functions/
Oh sorry kemoi, i didnt see your input function.
In that case, you have to use pass by reference as keene said.
Topic archived. No new replies allowed.