Jump to content
  • Advertisement
Sign in to follow this  
Twinsen2

Switch Statement Not Switching :)

This topic is 4830 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

hey....LOL another post by me.... Just seems that this is such a great place to receive help from. umm...down 2 busneiss. I have like a ranking system that checls through and outputs your rank dependent upon your total kill count. I have all of the advanced things working like link lists ect and openGL is set up and stuff like that, but these little basic things really get me stuck. I dn...maybe i'm always looking 4 the harderst way. heres my switch
int rank(int totalKills)
{
	
	/// To deterimne what rank a player is we can use a simple- "yeah right" switch statement that will allocate a rank.

	
	switch (totalKills)
	{
		case (>=1):
				cout << "Your Rank is Trainee.."; << "\n";
				
		break;

		case  (<500 != >1):
				cout << "Your Rank is a Soldier.."; << "\n";
		break;

		case (<999 != >500):
				cout << "Your Rank is a Captain.." << "\n";
		break;

		case (>=1000):
				cout << "Your Rank is a Genreal.." << "\n";
		break;

		default:
				cout << "Error You Have Not Been Ranked" << "\n";
	}
				
}
Okay now in my opionion it should work, the cases r set up but when i run it ii get the following errors:
error C2059: syntax error : '<'
I basicly get this error for every time theres a greater than or less than symbol
 warning C4065: switch statement contains 'default' but no 'case' labels
i get this error aswell and i have been looking 4 the past 2 hours for a fix, but cant seem to find ne, all my programing books have got the case equal to a certin vale: case 1 ect. Just wondering if its possible 2 do this with a switch and if not what would the best way be?? thankz once again

Share this post


Link to post
Share on other sites
Advertisement
Guest Anonymous Poster
You should remove the semicolons at the end of your strings, the ones before <

Share this post


Link to post
Share on other sites
I didn't know you could do that with a switch statement.

I recommend just using if...else if...else

int rank(int totalKills)
{
/// To deterimne what rank a player is we can use a simple- "yeah right" switch statement that will allocate a rank.


if (totalKills >= 1000)
{
cout << "Your Rank is a General.." << "\n";
}
else if (totalKills >= 500)
{
cout << "Your Rank is a Captain.." << "\n";
}
else if (totalKills >= 100)
{
cout << "Your Rank is a Soldier.."; << "\n";
}
else
{
cout << "Your Rank is Trainee.."; << "\n";
} // your ranking for soldier and trainee didn't make sense.

}





The purpose of switch statements is like your programming book shows. It is just an elaborate version of if...else if...else. Except that the switch can only be one value. This is why you can't use switch for strings (there are ways around this, but it is not built in).

Share this post


Link to post
Share on other sites
Quote:
Original post by nprz
I didn't know you could do that with a switch statement.

I recommend just using if...else if...else
*** Source Snippet Removed ***

The purpose of switch statements is like your programming book shows. It is just an elaborate version of if...else if...else. Except that the switch can only be one value. This is why you can't use switch for strings (there are ways around this, but it is not built in).


Once again, this friendly community has come to my saviour.
Thankz so much 4 ur help, working great now.
Umm yeah, i used a switch statement cause they look more arranged, and i thought i may have made a stupid error, but the if, then & else statements r working fine, so switches r just good for menu navigation in dos??

neway stuff the switch statements and thankz a million

Share this post


Link to post
Share on other sites
Quote:
Original post by Twinsen2
Quote:
Original post by nprz
I didn't know you could do that with a switch statement.

I recommend just using if...else if...else
*** Source Snippet Removed ***

The purpose of switch statements is like your programming book shows. It is just an elaborate version of if...else if...else. Except that the switch can only be one value. This is why you can't use switch for strings (there are ways around this, but it is not built in).


Once again, this friendly community has come to my saviour.
Thankz so much 4 ur help, working great now.
Umm yeah, i used a switch statement cause they look more arranged, and i thought i may have made a stupid error, but the if, then & else statements r working fine, so switches r just good for menu navigation in dos??

neway stuff the switch statements and thankz a million


There are many places where a switch statement would be appropriate. I use them in my game for handling keys, for determining what type of packet is sent (using the first character to identify it), for separating my GUI controls.

Share this post


Link to post
Share on other sites
Quote:
Original post by nprz
Quote:
Original post by Twinsen2
Quote:
Original post by nprz
I didn't know you could do that with a switch statement.

I recommend just using if...else if...else
*** Source Snippet Removed ***

The purpose of switch statements is like your programming book shows. It is just an elaborate version of if...else if...else. Except that the switch can only be one value. This is why you can't use switch for strings (there are ways around this, but it is not built in).


Once again, this friendly community has come to my saviour.
Thankz so much 4 ur help, working great now.
Umm yeah, i used a switch statement cause they look more arranged, and i thought i may have made a stupid error, but the if, then & else statements r working fine, so switches r just good for menu navigation in dos??

neway stuff the switch statements and thankz a million


There are many places where a switch statement would be appropriate. I use them in my game for handling keys, for determining what type of packet is sent (using the first character to identify it), for separating my GUI controls.




yeah i have a massive switch statement on my openGL engine, 2 handle the [VK_KEY] paramater, i also have switches controling my message pump thingy.

i was just supriesed that they couldn't handle ">=" type things
neway thankz again man 4 help

Share this post


Link to post
Share on other sites
Quote:
Original post by Twinsen2
i was just supriesed that they couldn't handle ">=" type things
neway thankz again man 4 help

It would not make any sense if that were possible in your case. You switch an expression of a certain type (in your case: int). Then you have to "case" it with a lot of other const expressions which have to be of the same type (int), but you try to "case" it with something that may look like an expression of the type bool at first glance. But (>=1) is not even an expression, how would that compile? It's not possible.

Share this post


Link to post
Share on other sites
Quote:
Original Post by Oluseyi
case labels must be integral constants.


This is, I think, the single most important point. To quote the standard:

Quote:

case constant-expression :

where the constant-expression shall be an integral constant expression.


Could they make it any clearer? ;)

Honestly though, switch statements are in C (on consequently in C++) because they are very easy to implement in assembly language and C was big on things like that. (i.e. 1 XOR operation and 1 Jxx operation per case) It wasn't big on more complex features. And what the heck, you don't need em' - you have if statements. Switch statements are a special case feature in many respects.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!