Stuck on switch menu options?

Pages: 123
This is the output I'm supposed to get to:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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 (10% tax included)

Enter your age? 18
Enter your gender(m/f)? f
Enter your full name: Hilary Clinton
     Hello Mrs. Hilary Clinton
Your membership fee is 165.50 (10% tax included)

Enter your age? 10
Enter your gender(m/f)? m
Enter your full name: George W. Bush
     Hello Mr. George W. Bush
Your membership fee is 55.00 (10% tax included)
Last edited on
so I do that for each one right and name it differently something like this:


No, you do not need to put it in a different variable. They all can go in the same variable.

You just have to put that line in the appropriate if/switch so it will only get executed when the circumstances are correct.

Example:

1
2
3
4
5
6
7
8
9
10
11
12
if( Age < 13 )
{
    somevar = CHILDREN_MALE * 0.10;  // somevar = 5 if they are less than 13 years old (1-12)
}
else if( Age < 20 )
{
    somevar = TEENAGER_MALE * 0.10; // somevar = 15 if they are 13-19 years old
}
else
{
    somevar = ADULT_MALE * 0.10;  // somevar = 27.5 if they are 20+ years old
}




EDIT: of course, you will also have to check their gender to choose MALE/FEMALE prices... I didn't want to give you the exact answer.
Last edited on
Oh ok, so what about female ? Do I do the same thing and add female?
You can use a switch to check to see if they're male/female.
So how would I do switch then?
I gave an example of how switch works previously:

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;
}


Fill in your own info. I don't want to give the direct answer. You should be able to figure it out from there.
So are you saying I can't do what I was doing before like:
1
2
3
4
switch(Age)
	{
		case 'm': CHILDREN_MALE+somevar; 
	 }
Ok, but are you saying that switch cannot have Age inside it?
That's close, but it's still wrong.

You're still checking their age. Male/female has absolutely nothing to do with their age.

'm' is not an age.
Last edited on
Oh, should it be switch(gender) ?
Yes. =)
Also, please don't use 'somevar' in your program. I just used that as a dummy name. Your variable names should be more meaningful.
Still didn't work though
Can't see what you're doing wrong without seeing the code.
Wait do I put the if inside?
I'll change somevar after I'm done but this is what i have so far:

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
#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, 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; 
	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;  

	if( Age < 13 )
	{
    somevar = CHILDREN_MALE * 0.10;  // somevar = 5 if they are less than 13 years old (1-12)
	}
	else if( Age < 20 )
	{
    somevar = TEENAGERS_MALE * 0.10; // somevar = 15 if they are 13-19 years old
	}
	else
	{
    somevar = ADULTS_MALE * 0.10;  // somevar = 27.5 if they are 20+ years old
	}
	switch(gender)
	{
		case 'm': CHILDREN_MALE+somevar; 
		case 'f': CHILDREN_FEMALE+somevar; 
    }

	system ("pause");
	return 0; 
}
You can do it either way. The if can go inside the switch or the switch can go inside the if. It doesn't really matter.
You're just copy/pasting my code. If you do that your program will not work correctly. I'm intentionally giving you code that will not work because I don't want you to copy/paste.

The stuff I'm posting is conceptual. It's to show you how these parts of the language work. You have to understand them and adapt them to your problem on your own.


EDIT: My goal is not to solve this problem for you, but to help you understand how if/switch work so that you can solve the problem yourself.
Last edited on
I understand the if else part but I don't get the way to put it inside
Break it into a smaller chunk:

1
2
3
4
5
6
7
8
9
if( Age < 13 )
{
     /*
    Inside this block of code, you know that the user is between ages 1-12.

    So what do you need to do here in order to calculate the cost?  How do you calculate the
      cost for a 1-12 year old?
    */
}
Last edited on
Pages: 123