Calling function within if or switch statements

Hi all! I'm writing some code for a "casino" game but have hit a snag with using multiple functions (which is required). I'm able to go to the menu from main, and upon entering 1 to attempt to go to the High-Low Game I'm able to change OptionSelect to 1, and it prints as 1 in main after menu is called, but I'm not sure what I'm doing wrong to use OptionSelect to decide what game to go to. Neither the if statement nor the switch statement allow me to call the HighLow function. I'm sure I'm just missing something silly, but I haven't been able to figure this out for a while. Thanks!
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
#include <iostream>

#include <cstdlib>

#include <iomanip>

#include <ctime>

using namespace std;

//Prototypes
int menu(int &UserMoney, int &OptionSelect, int ErrorCheck, int betting);
int HighLow(int &UserMoney, int HighLowGuess, int ErrorCheck, int betting);
void Roulette();
void Blackjack();
void SetBetting();


int main()
{
	srand(time(NULL));
	
	int UserMoney = (rand() % 100) + 100;
	int OptionSelect = 0;
	int ErrorCheck = 0;
	
	// betting variables
	int betting = 5;
	
	//game variables
	int HighLowGuess;
	
	
	menu(UserMoney, OptionSelect, ErrorCheck, betting); 
	cout << OptionSelect << endl; //to test for optionselect being modified in menu


/*	switch(OptionSelect)
	{
		case 1:
			HighLow(UserMoney, HighLowGuess, ErrorCheck, betting);
	}
*/	
		
	
/*	if(OptionSelect == 1) //called OptionSelect is not activating the if statement
	{
		HighLow(UserMoney, HighLowGuess, ErrorCheck, betting);
	} */
}

int menu(int &UserMoney, int &OptionSelect, int ErrorCheck, int betting)
{
	cout << "You have $" << UserMoney << "." << endl;
	
	//Loop for error in input
//	while(ErrorCheck == 0)
	
	//Option Select
	cout << "What would you like to do (Enter 1-5)" << endl;
	cout << "1) Play High-Low" << endl << "2) Play Roulette" << endl << "3) Play 21" << endl <<  "4) Set Betting Amount" << endl << "5) Exit" << endl;
	cin >> OptionSelect;
	//check to be sure input is a number
	if((!(OptionSelect > 0)) || (OptionSelect > 5) )
	{
		cout << "Please Only Enter a Number Between 1 and 5." << endl;
	}
	else
	{
		ErrorCheck = ErrorCheck + 1;
	}
//	}
	}

int HighLow(int &UserMoney, int HighLowGuess, int ErrorCheck, int betting)
{
	while (ErrorCheck = 0)
	{
	int RandNum1 = (rand() % 10) + 1;
	cout << "The number is " << RandNum1 << "." << endl << "Will the next number be higher or lower (1 or 2)?" << endl << "1) Higher" << endl << "2)Lower" << endl;
	cin >> HighLowGuess;
	
	int RandNum2 = (rand() % 10) + 1;
	
	if ((HighLowGuess = 1) && (RandNum1 < RandNum2) )
	{
		UserMoney = UserMoney + (betting * 1);
		cout << "The next number was " << RandNum2 << "." << endl << "Congratulations! You got it right." << endl;
		cout << "Your new balance is $" << UserMoney << "." << endl; 
	}
	else if ((HighLowGuess = 2) && (RandNum1 > RandNum2) )
	{
		UserMoney = UserMoney + (betting * 1);
		cout << "The next number was " << RandNum2 << "." << endl << "Congratulations! You got it right." << endl;
		cout << "Your new balance is $" << UserMoney << "." << endl; 
	}
	else
	{
		cout << "Error: Please only enter either 1 or 2." << endl;
	}
	}
}
@Hippotopamus

Lines 85 and 91 are incorrect. You are using a single equal sign, which is for assigning a value to a variable, when you should be using a double equals, ==, to check the value of the variable instead. The same goes for line 77. You have it right on line 57, though you're not using it right now.


Question though.. What's going to happen if the RandNum2 number matches the HighLowGuess? Looks to me that the else statement will be printed, even though the use entered a 1 or a 2. Might want to check for this possibility and print a statement.
Last edited on
Thanks for catching that! My big issue is that the program never even runs HighLow... it runs menu and outputs the new OptionSelect, but the program exits there.
You have $197.
What would you like to do (Enter 1-5)
1) Play High-Low
2) Play Roulette
3) Play 21
4) Set Betting Amount
5) Exit
1
1

--------------------------------
Process exited after 1.854 seconds with return value 0
Press any key to continue . . .
@Hippotopamus

Lines 38 thru 49, are not usable, as you made them remarks, with the /* */. The function NEVER gets called. Remove them on lines 38 and 43.
I have them set into comments while I tried the other, neither the Switch nor If statement change the output, which is the root of my problem.
@Hippotopamus

On line 34, you print the menu and get the OptionSelect value, then on line 35, you print the value. Nowhere do you do anything with it. Where in your code, do you call the function, HighLow? Plus, you declare the function with an int, but do not return a value. The HighLow function and menu function, should be declared with void instead, since they return nothing.
I don't know why (I had tried both the switch and if statement after changing all the = I needed as ==), but now the switch statement works after trying again... Thanks!
Topic archived. No new replies allowed.