Public Group

# learning the switch statement

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

## Recommended Posts

Howdy again. I was just wondering when using the switch statement why I can't get number to be accepted. When I run the program below, every time I enter a number (either 1 or 2) it goes directly to the default statement. I have looked at a number of other programs on the internet that do what I am trying to do but for some reason this specific example doesn't work. I am pretty sure its just some simple misunderstanding of how switch works. Also, though this code is short, is it written somewhat neatly? I want to start writing code as readable as possible so its more of a habit than anything else. Thanks guys!
#include <iostream>
#include <string>
using namespace std;

int main() {
int strength = 25;
int estrength = 28;
int decision;
float damage = 0.000;
cout << "This is basic stat idea\n\n" << endl;
cout << "Choose a move 1 or 2" << endl;
cin >> decision;
switch (decision) {
case '1':
cout << "Flying Butt Tweezers1\n\n\a" << endl;
damage = (strength * .025) + (.015 * estrength);
cout << damage << endl;
break;
case '2':
cout << "Flying Butt Tweezers2\n\n\a" << endl;
damage = (strength * .0325) + .01;
cout << damage << endl;
break;
default:
cout << "You did not choose a correct integer number" << endl;
break;

}
return 0;

}


##### Share on other sites
You're reading the number as an int, but switching on the charater values. Try removing the single quotes from the case labels.

##### Share on other sites
case '1':
case '2':
You are checking against the ascii value of the character 1 and 2, not the number one an two. Change it to this:
case 1:
case 2:

##### Share on other sites
read it and was goin to anwser but someone beat me to it =/

##### Share on other sites
thanks guys. that makes sense I suppose. I guess I still don't know enough about the syntax of the switch to use it fully....doh.

##### Share on other sites
Quote:
 Original post by killninethanks guys. that makes sense I suppose. I guess I still don't know enough about the syntax of the switch to use it fully....doh.

Well when you use the case operator it is asking for the same type as the variable you are switch()ing. In this case you are checking an integer so the case statement should have an integer in it a la "case 1:". If you are checking a character, then the case would have a character a la "case '1':".

Hope this helps.

##### Share on other sites
ROFL, "Flying Butt Tweezers"

I had a problem with single-quotes around integers and stuff. A case '1': would check to see if the value is something around 46, I believe. Since single-quotes represent the CHARACTER value...

##### Share on other sites
Your switch statement syntax is just fine; the problem is understanding how the language handles types.

As you are probably aware, a switch statement expects a variable of an integral numeric type (such as int). This is an early-history language decision; this restriction makes it easier for the compiler to optimize the generated code in certain cases (in particular, when the 'case' values are close to one another). Depending on circumstances, the assembly generated by a switch statement could vary a lot in appearance, even though it will always *behave* the same way in the end.

When you put a single character inside single quotes, this creates what is called a "character literal". That is, a value of type char, which represents the text of the character inside the quotes.

Now here is the evil part. The C++ typing system does some implicit conversions between numeric types (especially between integral types), because logically they're the same thing really, just with different amounts of space allocated to store the value. However, 'char' is a numeric type too.. This is a consequence of the fact that at the machine level it's all just bits anyway, and it makes more sense to interpret a character as an integral number than a floating point number. The number here is like a "character ID", which is translated into real characters for display by looking it up in the current font. Fortunately, the (basic) translation is standardized: google "ASCII".

Long story short, '0' is a char (which is an integral type) which holds the ASCII value corresponding to the text character "0" - the value is 48. So if you are reading an int, that's what the user would have to type - "48". This is because the stream reading works with templates, which are smart enough to know the difference between exact numeric types, and do something different for each for convenience - in particular, it just reads one byte when reading a char, so that you can treat those variables as a textual type.

So you either need to compare to integral types in your switch (by dropping the quotes), or read in a character (by giving decision a char type). Both of these of course have their disadvantages: reading in a char won't let you use values over 9 (although it will let you use letters and punctuation), and reading in an int will mess up if the user types something non-numeric.

(And if you think that's bad, wait till you have to deal with Unicode ;) )

##### Share on other sites
This is probably where some sort of error handling would come in. Mostly, I wanted to work in a random number generator in here somewhere (which I did in a revision of the code). However, I got stuck on a simpler issue beforehand. I havn't done much error handling in C++, and in Java it was easy because the error that would occur was given a set course of action through a try-block. Anyhow, I suppose error handling is something I will eventually get to in my studies. On with the basics!

1. 1
Rutin
22
2. 2
3. 3
4. 4
5. 5

• 9
• 9
• 9
• 14
• 12
• ### Forum Statistics

• Total Topics
633307
• Total Posts
3011289
• ### Who's Online (See full list)

There are no registered users currently online

×