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.
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.
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..
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?