Stuck on switch menu options?

Pages: 123
Ok so I did my own one like this as you said:

1
2
3
4
5
6
7
8
9
10
11
if (Age > 13)
{
     if (gender == 'M' || gender == 'm') 
        {
            Mcost = 50.00;
         }
     else  
        {
           Mcost = 55.00; 
        }
}
That's great! That will work.

Although I would use your CHILDREN_MALE and CHILDREN_FEMALE constants rather than using magic numbers.


It also doesn't use switch, but that's fine. You can do the same thing with a switch -- I just wasn't sure if switch was a requirement or not. =) But yeah that is functional.


EDIT:

Actually I just noticed you are checking if Age > 13 (greater), not < 13 (less)


EDIT 2:

With that, I'm going to have to call it a night. It's very late by me. I'll check this thread tomorrow.
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
if (Age <= 12)
{
     if (gender == 'M' || gender == 'm') 
        {
            Mcost = 50.00;
         }
     else  
        {
           Mcost = 55.00; 
        }
}
else if (Age > 13)
{
Switch is a requirement though
Ok so I did do it with if else but switch is supposed to be required in my program. Don't know how to add it, can you help please? Don't have too much more time, have til about 12:30 pacific time. How would I be able to add switch into this?

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 <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, somevar;
	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; 
}
so I did do it with if else but switch is supposed to be required in my program. Don't know how to add it, can you help please?


Take a look at my previous example of a switch statement:

1
2
3
4
5
6
7
8
9
10
switch( <expression> )
{
   case <possible_value>:
      // do something if <expression> == <possible_value>
      break;

   case <another_possible_value>:
      // do something if <expression> == <another_possible_value>
      break;
}



Compare that to a similarly structured if/else statement:

1
2
3
4
5
6
7
8
if( <expression> == <possible_value> )
{
    // do something if <expression> == <possible_value>
}
else if( <expression> == <another_possible_value> )
{
    // do something if <expression> == <another_possible_value>
}



EDIT:

Also, you REALLY should be using your CHILDREN_MALE, CHILDREN_FEMALE, etc constants rather than using magic numbers in your code. Like... that's kind of important.

Hint: don't worry about the 10% tax when determining the base price. Tax can be added later.
Last edited on
Topic archived. No new replies allowed.
Pages: 123