Switch Statement to make Menu

I think I was supposed to use swich statement to construct a menu but I did it like this, how would I do it using switch and members of <iomanip> ?

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

using namespace std;

const float CHILDREN_MALE=50.00;
const float CHILDREN_FEMALE=55.00;
const float TEENAGERS_MALE=150.00;
const float TEENAGERS_FEMALE=155.00;
const float ADULTS_MALE=275.00;
const float ADULTS_FEMALE=250.00;

int main()
{
	int Age;
	float Mcost-0.0, total=0.0;
	char Gender; 
	cout << fixed << showpoint << setprecision (2);
	
	//Display Menu
	cout << "-------------Fullerton Health Club--------------" << endl;
	cout << "Children (Age 1-12) " << endl;
	cout << setfill ('.');
	cout << left << setw(15) << "\tMale" << right << setw(6) << CHILDREN_MALE << endl; 
	cout << left << setw(15) << "\tFemale" << right << setw(6) << CHILDREN_FEMALE << endl; 
	cout << "Teenagers (Age 13-19)" << endl;
	cout << left << setw(15) << "\tMale" << right << setw(6) << TEENAGERS_MALE << endl;
	cout << left << setw(15) << "\tFemale" << right << setw(6) << TEENAGERS_FEMALE << endl;
	cout << "Adults (Age > 19)" << endl; 
	cout << left << setw(15) << "\tMale" << right << setw(6) << ADULTS_MALE << endl;
	cout << left << setw(15) << "\tFemale" << right << setw(6) << ADULTS_FEMALE << endl;
make the user to select option on menu;

1
2
3
4
5
6
7
8
9
10
11
12
13
switch ( selection ) 
{
case 1: 
// codes for menu 1
break;
case 2:
// codes for menu 2
break;
case 3:
// codes for menu 3
break;
default break;
}
So what do I put for the cases? Do I put the Children, Teenagers, Adults and what about the Male and female for each of them, what happens with those?
How will you know which menu item your user selected? In your code where you display the menu options you need to also display a number, or something, that the user can enter to select the menu item.

like
1
2
3
4
5
6
7
8
9
 children (age 1-12)
 1. Male
 2. Female
Teenagers (Age 13-19)
 3. Male
 4. Female
Adults (Age > 19)
 5. Male
 6. Female


These numbers are the cases' in the switch. When the user enters a number the appropriate case is executed.

eg;
1
2
3
4
5
6
7
8
switch ( selection ) 
{
case 1: 
  Mcost = CHILDREN_MALE;
  break;
case 2:
  Mcost = CHILDREN_FEMALE;
  break;



Also: float Mcost-0.0 wont compile, it should be float Mcost = 0.0
Last edited on
How do I make a switch case statement so that I can choose?

With 3 outputs like this:

1
2
3
4
5
Enter your age? 48
Enter your gender(m/f)? m
Enter your full name: Barack H Obama
     Hello Mr. Barack H Obama
Your membership fee is 302.50
I'm not sure how to make the age < a number using switch statement?
So far I have something like this:
I don't get why

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

using namespace std;

const float CHILDREN_MALE=50.00;
const float CHILDREN_FEMALE=55.00;
const float TEENAGERS_MALE=150.00;
const float TEENAGERS_FEMALE=155.00;
const float ADULTS_MALE=275.00;
const float ADULTS_FEMALE=250.00;

int main()
{
	int Age, name;
	float Mcost=0.0;
	float total=0.0; 
	char gender;
	cout << fixed << showpoint << setprecision (2);
	
	//Display Menu
	cout << "-------------Fullerton Health Club--------------" << endl;
	cout << "Children (Age 1-12) " << endl;
	cout << setfill ('.');
	cout << left << setw(15) << "\tMale" << right << setw(6) << CHILDREN_MALE << endl; 
	cout << left << setw(15) << "\tFemale" << right << setw(6) << CHILDREN_FEMALE << endl; 
	cout << "Teenagers (Age 13-19)" << endl;
	cout << left << setw(15) << "\tMale" << right << setw(6) << TEENAGERS_MALE << endl;
	cout << left << setw(15) << "\tFemale" << right << setw(6) << TEENAGERS_FEMALE << endl;
	cout << "Adults (Age > 19)" << endl; 
	cout << left << setw(15) << "\tMale" << right << setw(6) << ADULTS_MALE << endl;
	cout << left << setw(15) << "\tFemale" << right << setw(6) << ADULTS_FEMALE << endl;

	//Ask for age, gender, and full name
	cout << "Enter your age? "; 
	cin >> Age; 
	cout << "Enter your gender(m/f)? ";
	cin >> gender; 
	cout << "Enter your full name: "; 
	cin >> name; 
	cout << "  Hello" << (gender == 'm' ? " Mr. " : " Mrs.") << name << "," << endl;
	
	cout << "Your membership fee is " << float (Mcost) << "(10% tax is included)" << endl;  

	system ("pause");
	return 0; 
}
Last edited on
Help???
You cannot use a switch to check a range. You need if:
1
2
3
4
5
6
if(Age < 13)
	cout << "Your membership fee is " << (gender == 'm' ? CHILDREN_MALE : CHILDREN_FEMALE) << "(10% tax is included)" << endl;  
else if(Age < 20)
...
else
...
This is a duplicate thread. I've been helping this guy here:

http://www.cplusplus.com/forum/general/155618/
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
#include <iostream> 
#include <iomanip> 
#include <string>

using namespace std;

const float CHILDREN_MALE=50.00;
const float CHILDREN_FEMALE=55.00;
const float TEENAGERS_MALE=150.00;
const float TEENAGERS_FEMALE=155.00;
const float ADULTS_MALE=275.00;
const float ADULTS_FEMALE=250.00;

int main()
{
	int Age = 0;
	std::string name;
	float Mcost=0.0;
	float total=0.0; 
	char gender;
	cout << fixed << showpoint << setprecision (2);
		
	//Display Menu
	cout << "-------------Fullerton Health Club--------------" << endl;
	cout << "Children (Age 1-12) " << endl;
	cout << setfill ('.');
	cout << left << setw(15) << "\tMale" << right << setw(6) << CHILDREN_MALE << endl; 
	cout << left << setw(15) << "\tFemale" << right << setw(6) << CHILDREN_FEMALE << endl; 
	cout << "Teenagers (Age 13-19)" << endl;
	cout << left << setw(15) << "\tMale" << right << setw(6) << TEENAGERS_MALE << endl;
	cout << left << setw(15) << "\tFemale" << right << setw(6) << TEENAGERS_FEMALE << endl;
	cout << "Adults (Age > 19)" << endl; 
	cout << left << setw(15) << "\tMale" << right << setw(6) << ADULTS_MALE << endl;
	cout << left << setw(15) << "\tFemale" << right << setw(6) << ADULTS_FEMALE << endl;

	//Ask for age, gender, and full name
	cout << "Enter your age? "; 
	cin >> Age; 
	cout << "Enter your gender(m/f)? ";
	cin >> gender; 
	cout << "Enter your full name: "; 
	cin >> name; 
	
	// if age is greater than or equal to 1 or age is less than or equal to 12 and gender is male
	if((Age >= 1 || Age <=12) && (gender == 'm' || gender == 'M'))
	{
		cout << "Hello Mr " << name << " ," << endl;
		cout << "Your membership fee is " << CHILDREN_MALE << "(10% tax is included)" << endl;  
	}
	else if((Age >= 1 || Age <=12) && (gender == 'f' || gender == 'F'))
	{
                cout << "Hello Ms " << name << " ," << endl;
		cout << "Your membership fee is " << CHILDREN_FEMALE << "(10% tax is included)" << endl;  
	}	
	// just add more conditions here.

	system ("pause");
	return 0; 
}
Last edited on
I was on the other thread too but I need help fast so I posted here as well.
I did my program like this but switch statement is required so how would I incorporate it?

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

using namespace std;

const float CHILDREN_MALE=50.00;
const float CHILDREN_FEMALE=55.00;
const float TEENAGERS_MALE=150.00;
const float TEENAGERS_FEMALE=155.00;
const float ADULTS_MALE=275.00;
const float ADULTS_FEMALE=250.00;

int main()
{
	int Age;
	string type, name;
	float Mcost=0.0, 
	float total=0.0; 
	char gender;
	cout << fixed << showpoint << setprecision (2);
	
	//Display Menu
	cout << "-------------Fullerton Health Club--------------" << endl;
	cout << "Children (Age 1-12) " << endl;
	cout << setfill ('.');
	cout << left << setw(15) << "\tMale" << right << setw(6) << CHILDREN_MALE << endl; 
	cout << left << setw(15) << "\tFemale" << right << setw(6) << CHILDREN_FEMALE << endl; 
	cout << "Teenagers (Age 13-19)" << endl;
	cout << left << setw(15) << "\tMale" << right << setw(6) << TEENAGERS_MALE << endl;
	cout << left << setw(15) << "\tFemale" << right << setw(6) << TEENAGERS_FEMALE << endl;
	cout << "Adults (Age > 19)" << endl; 
	cout << left << setw(15) << "\tMale" << right << setw(6) << ADULTS_MALE << endl;
	cout << left << setw(15) << "\tFemale" << right << setw(6) << ADULTS_FEMALE << endl;

	//Ask for age, gender, and full name
	cout << "Enter your age? "; 
	cin >> Age; 
	cout << "Enter your gender(m/f)? ";
	cin >> gender; cin.ignore();
	cout << "Enter your full name: "; 
	getline (cin, name); 
	cout << "  Hello" << (gender == 'm' ? " Mr. " : " Mrs.") << name << ",";
	getline(cin, type);

	if (Age <= 12)
	{
     if (gender == 'M' || gender == 'm') 
        {
            Mcost = 55.00;
         }
     else  
        {
           Mcost = 60.50; 
        }
	}
	else if (Age >= 13)
	{
	     if (gender == 'M' || gender == 'm') 
        {
            Mcost = 165.00;
         }
		else  
        {
           Mcost = 170.50; 
        }
		if (Age > 19)
	{
		 if (gender == 'M' || gender == 'm') 
        {
            Mcost = 302.50;
         }
     else  
        {
           Mcost = 275.00; 
        }
	}
	}
	
	cout << "Your membership fee is " << float (Mcost) << "(10% tax is included)" << endl;  

	system ("pause");
	return 0; 
}
...but switch statement is required...


If you need to switch on the menu, look at my earlier answer above. other than that...

You can only switch on constant values, and you can only test equality, that means you wont be able to switch on the age because you need to test a range (like 13-19).
Your only option left is to switch on the gender.

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
switch(gender)
{   
   case 'M':
   case 'm':

      if  (age <= 12)
         Mcost = CHILDREN_MALE;
      else if (age <= 19)
         Mcost = TEENAGERS_MALE;
      else
         Mcost = ADULTS_MALE;

      break;

   case 'F':
   case 'f':

      if  (age <= 12)
         Mcost = CHILDREN_FEMALE;
      else if (age <= 19)
         Mcost = TEENAGERS_FEMALE;
      else
         Mcost = ADULTS_FEMALE;

      break;
}

@Jaybob: I'm trying not to give out direct solutions. =/

How is he going to learn if we do the work for him?
Topic archived. No new replies allowed.