Jump to content
  • Advertisement
Sign in to follow this  
Kalner

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.

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

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 this post


Link to post
Share on other sites
Advertisement
'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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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.

Share this post


Link to post
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 this post


Link to post
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.

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.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!