Public Group

# Switch statement problem. (C++)

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

## Recommended Posts

When I use the switch statement, and after I made the first case, I decided to try out if it would work but when entering the letter E beacause that is the only case which exists I dont get anything after press E than enter. Can someone explain. #include <iostream> #include <cstdlib> #include <ctime> using namespace std; int main() { int dif; srand(time(0)); cout << "Please Select a Difficllty E = Easy M = Medium H = Hard" <<endl; cin >> dif; int gn; switch(dif){ case 'e || E': int number = 1+(rand()%10); cout << "Guess a number beetween 1 and 10" <<endl; cin >> gn; if(gn == number){ cout << "You won!" <<endl; } break; } } 

Thanks.

##### Share on other sites
'e || E' is not a valid case. Use fallthrough for this, or alternatively, change the character casing

 switch(diff) { case 'e': case 'E': //... 

 switch(std::use_facet< std::ctype<char> >(std::locale()).tolower ( diff )) { case 'e': // ... 

##### Share on other sites

switch(std::use_facet< std::ctype >(std::locale()).tolower ( diff )) { case 'e': // ...

Seriously? You have to do all of that just to get "toLower" in C++? Geez....

##### Share on other sites

[quote name='Washu' timestamp='1329609299' post='4914350']
switch(std::use_facet< std::ctype >(std::locale()).tolower ( diff )) { case 'e': // ...

Seriously? You have to do all of that just to get "toLower" in C++? Geez....
[/quote]
No. I would actually just call
 switch(std::tolower(choice)) { case 'e': // ... 
in cctype or
 switch(std::tolower(choice, std::locale())) { case 'e': // ... 
in locale

##### Share on other sites
I knew there was an easier way to do it, but I figured you were using C++ idioms or something you felt was necessary to show

No worries. C++ is sane again, lol.

##### Share on other sites
Even after I do that nothing happens. No errors no nothing, runs program until I get the promt for difficulty.

##### Share on other sites
Part of the reason you're not getting any results is that you're "choice" is an integer, use a char when you want a char.

Thank you =D

##### Share on other sites
Let me ask you this. If number = 9 and I guess 7, then what happens after that in your program? (this is a HINT)

Also, (and this has nothing to do with your problem but still should be done) where in the world is your  return 0; ?

##### Share on other sites

Thank you =D

Just to explain a bit...

In C++ std::cin is typesafe on the input. The character 'e' is not a digit, thus std::cin.fail() becomes true and your integer is left at whatever default value it had (which is garbage).

When you say "std::cin>>mychar" and mychar is of type char, then it can be any valid ASCII character. In which case you get the result expected.

If you had stuck something like
std::cout<<std::boolalpha<<std::cin.fail()<<std::endl;
after your cin>>dif you would have seen the value "true" meaning it had failed.

1. 1
2. 2
Rutin
18
3. 3
4. 4
5. 5

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

• Total Topics
631431
• Total Posts
3000040
×