Sign in to follow this  

Switch statement problem. (C++)

This topic is 2123 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. [code]#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;
}
}
[/code]

Thanks.

Share this post


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

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

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

Share this post


Link to post
Share on other sites
[quote name='Alpha_ProgDes' timestamp='1329609540' post='4914352']
[quote name='Washu' timestamp='1329609299' post='4914350']
[CODE]switch(std::use_facet< std::ctype >(std::locale()).tolower ( diff )) {
case 'e':
// ...[/CODE]
[/quote]
Seriously? You have to do all of that just to get "toLower" in C++? Geez....
[/quote]
No. I would actually just call
[code]
switch(std::tolower(choice)) {
case 'e':
// ...
[/code]
in cctype or
[code]
switch(std::tolower(choice, std::locale())) {
case 'e':
// ...
[/code]
in locale
:)

Share this post


Link to post
Share on other sites
[quote name='Kalner' timestamp='1329610196' post='4914358']
Thank you =D
[/quote]
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
[code]std::cout<<std::boolalpha<<std::cin.fail()<<std::endl;[/code]
after your [code]cin>>dif[/code] you would have seen the value "true" meaning it had failed.

Share this post


Link to post
Share on other sites
[quote name='Washu' timestamp='1329610107' post='4914357']
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.
[/quote]
Wouldn't 'E' or 'e' just be converted to its ASCII number equivalent automatically in this situation?

Share this post


Link to post
Share on other sites
[quote name='Alpha_ProgDes' timestamp='1329610210' post='4914359']Also, (and this has nothing to do with your problem but still should be done) where in the world is your [CODE]
return 0;
[/CODE]?
[/quote]

There's an exemption for main. (It implicitly returns 0 unless otherwise specified.)

Share this post


Link to post
Share on other sites
[quote name='TheUnbeliever' timestamp='1329610632' post='4914364']
[quote name='Alpha_ProgDes' timestamp='1329610210' post='4914359']Also, (and this has nothing to do with your problem but still should be done) where in the world is your [CODE]
return 0;
[/CODE]?
[/quote]

There's an exemption for main. (It implicitly returns 0 unless otherwise specified.)
[/quote]
I know, but still.... it's just wrong not to have it.

Share this post


Link to post
Share on other sites

This topic is 2123 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this