Alright I'm currently in a C++ class and we just got to switch statements, and I can't get them to work for the life of me. I've googled my issue and my code seems to be exactly right, but no matter what my code doesn't work correctly, so I guess it isn't right.

For this program we need to ask a person if they are a member of a club. If they are a member, ask them their age, depending on the age charge them the right amount. If they aren't a member, charge them something else.

Attempted it multiple times with different code, but I'm stuck now. As far as I'm aware this is exactly what the book shows for code, so no idea why it isn't working. Here's my current code, hopefully you guys can explain to me what is wrong!

#include <iostream>
using namespace std;

int main()
{
	char membership = ' ';
	int age = 0;
	int fee = 0;
	membership = toupper(membership);

	cout << "Are you an existing member? (M or N) " ;
	cin >> membership;

	
	switch (membership)
	{
		case 'M':
		cout << "Current Age? ";
		cin >> age;

		if (age >= 65)
	{		
		fee = 5;
	}
		else if (age <= 64)
	{	
		fee = 10;
	}
		case 'N':
		fee = 20;
	}

	cout << "Seminar fee is: $" << fee << endl;
	
	system("pause");
}

I've compiled it and it seems to be working perfectly fine, could you maybe explain what's going wrong on your side?

The main issue I see with this is that you don't have break statements at the end of the cases. This means that the first case will 'fall through' to the second, which results in the fee always being set to 20.

commented: Thanks bro +1

I've compiled it and it seems to be working perfectly fine, could you maybe explain what's going wrong on your side?

When I run it, it doesn't ask me for age, and no matter if I enter M or N it says the fee is $0 which isn't right...

It still compiles without the break bring there but yes you are right about the $20, the fee will always be that amount, so just add break at the end of each case.

When I run it, it doesn't ask me for age, and no matter if I enter M or N it says the fee is $0 which isn't right...

How laughable. You must put the letter M in and not m. It's case sensitive, you have to adjust your code to make it more user-friendly.

commented: How Laughable. He tried to, but it's in the wrong spot. Be nice. -3

How laughable. You must put the letter M in and not m. It's case sensitive, you have to adjust your code to make it more user-friendly.

Dude I'm a beginner, I don't know this shit yet, course it's gonna be laughable to you, sorry. I thought that's what the toupper command was for though, what do I do to change that then?

Didn't even notice I didn't have my breaks, I feel dumb! lol

EDIT - Yeah it's in the wrong place, fixed that now. Code works great. Thanks guys.

While toupper() is the correct function to use, you want to call it after you've read in the new value, not before. Probably the easiers way to do it is

switch( toupper(membership))

The problem is that the char 'M' is not the same as the char 'm'. You have attempted to "normalize" the user's input, which is correct, but you don't have it in the correct spot. Move Line 9 to Line 13 so that the "normalization" occurs after the user's input.

Also, as mentioned before, you need to add the appropriate break; statements.

What I don't understand is that why the book gives an example which does not really show the efficiency of the switch-statement. This example could easily be done and easier to understand using if-else statement...

What I don't understand is that why the book gives an example which does not really show the efficiency of the switch-statement. This example could easily be done and easier to understand using if-else statement...

Because when you are learning, the easiest example is the easiest to understand. Doh!

Because when you are learning, the easiest example is the easiest to understand. Doh!

And do you think that this is the easiest example?????

And do you think that this is the easiest example?????

Who cares if it easiest? It's damm easy so highly understandable and gets the point across -- more so than an example that shows efficiency too.

Because when you are learning, the easiest example is the easiest to understand. Doh!

That's what you said and that's why I respond to your comment (look at the word "the easiest").

more so than an example that shows efficiency too

That's what I am looking for to be the answer. Thanks.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.