Menu Function not working
Apr 21, 2014 at 4:18pm UTC
Hey everyone.
I was wondering if I could get some help with this program. I'm trying to get the "get_menu_choice" function to.. well, get the user's menu choice. However, every time I run the program and input my choice, it outputs "Invalid selection. Try again.", no matter what I put (even if it is a valid input).
Can anyone help me out with this problem? (Also, any other comments on my current progress over all are welcome. The other function I'm using is used to sell seats in an auditorium.)
Any help is greatly appreciated.
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 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
# include <iostream>
# include <iomanip>
# include <cctype>
using namespace std;
char get_menu_choice(char choice);
void sell_seat(char seats[3][3][20]);
char choice;
char seats[3][3][20];
int main()
{
get_menu_choice(choice);
choice = toupper (choice);
switch (choice)
{
case 'S' :
sell_seat(seats);
break ;
case 'C' : cout << "You chose C!" << endl;
break ;
case 'D' : cout << "You chose D!" << endl;
break ;
case 'F' : cout << "You chose F!" << endl;
break ;
case 'Q' : cout << "Quit" << endl;
break ;
default : cout << "Invalid selection. Try again." << endl;
break ;
}
return 0;
}
char get_menu_choice(char choice)
{
cout.fill(' ' );
cout << setw(22) << "*** Main Menu ***" << endl
<< "S - Sell a Ticket." << endl
<< "C - Display Seating Chart." << endl
<< "D - Display Sales Summary - Day Listing" << endl
<< "F - Display Sale Summary - Floor Listing" << endl
<< "Q - Quit" << endl
<< "Your Choice: " ;
cin >> choice;
return choice;
}
void sell_seat(char seats[3][3][20])
{
int d, s, n;
seats[d][s][n] = '-' ;
cout << "Enter seat request by day (T)hursday, (F)riday, or (S)aturday" << endl
<< "followed by section (F)loor, (B)alcony, or (U)pper Balcony" << endl
<< "followed by seat number (1 - 20)." << endl;
cout << "Seat: " ;
do
{
cin >> d ;
d = toupper(d);
switch (d)
{
case 'T' :
d = 0;
break ;
case 'F' :
d = 1;
break ;
case 'S' :
d = 2;
break ;
default :
cout<<"Invalid choice try again" ;
break ;
}
} while ( choice != 'T' || choice != 'F' || choice != 'S' ) ;
do
{
cin >> s ;
s = toupper(s);
switch (s)
{
case 'F' :
s = 0;
break ;
case 'B' :
s = 1;
break ;
case 'U' :
s = 2;
break ;
default :
cout<<"Invalid choice try again" ;
break ;
}
} while ( choice != 'F' || choice != 'B' || choice != 'U' ) ;
do
{
cin >> n ;
if (n < 0 || n > 20)
cout << "Invalid seat number." << endl;
else
n-=1;
} while ( n <= 0 || n >= 20);
if (seats[d][s][n] == '*' )
cout << "This seat has been taken." << endl;
else
{
seats[d][s][n] = '*' ;
cout << "Congratulations! You got the seat." << endl;
}
}
Apr 21, 2014 at 4:27pm UTC
line 17 - you're calling the function but you're not storing the value that was returned for choice. On line 19 you're then trying to convert to upper whatever the uninitialized value in choice is.
Apr 21, 2014 at 4:38pm UTC
Thanks for the response.
I tried this, but it wasn't working either.
1 2 3 4 5 6 7 8 9
get_menu_choice(choice);
choice = menu_choice;
choice = toupper (menu_choice);
switch (choice)
{
(rest of code ....)
How can I fix my code?
Last edited on Apr 21, 2014 at 4:38pm UTC
Apr 21, 2014 at 4:48pm UTC
menu_choice by itself isn't defined - that's why lines with that won't work.
You want to assign the value returned by the function into your variable choice, so...
1 2
choice = get_menu_choice(choice);
choice = toupper(choice);
or
choice = toupper(get_menu_choice(choice));
Apr 21, 2014 at 4:59pm UTC
This worked. Thanks!
However, now I'm getting this every time I choose 'S':
2550 [main] william_rodriguez_prog5 4732 open_stackdumpfile: Dumping stack trace to william_rodriguez_prog5.exe.stackdump
Any advice for this?
Apr 21, 2014 at 5:05pm UTC
1 2
int d, s, n;
seats[d][s][n] = '-' ;
d, s and n have uninitialized values when you try to use them here.
Edit: also - check the syntax on passing multidimensional arrays.
http://www.cplusplus.com/doc/tutorial/arrays/
The format for a tridimensional array parameter is:
base_type[][depth][depth]
For example, a function with a multidimensional array as argument could be:
void procedure (int myarray[][3][4])
Notice that the first brackets [] are left empty, while the following ones specify sizes for their respective dimensions. This is necessary in order for the compiler to be able to determine the depth of each additional dimension.
Last edited on Apr 21, 2014 at 5:08pm UTC
Topic archived. No new replies allowed.