views:

222

answers:

5

Hi all , I'm beginner to c++ and writing a program that accepts user choices and acts according to it...my only problem is when the user enters Uppercase choice...the program treats it as it's a wrong choice...like if 'e' was a choice for entering a number..if the user entered 'E' the program won't display the "enter the number" message..how can i fix it ? i tried my best but i can't get it working.. Oh , and how can i add the Uppercase in Switch cases ? This is the part of the code that's responsible of taking user's choice and act according to it.

 #include <iostream>
 #include <cstring>
 using namespace std;

 int main(){

 char choice ;

 for(;;){
    do{
      cout << endl ;
      cout << "(e)nter." << endl ;
      cout << "(d)isplay." << endl;
      cout << "(u)pdate." << endl ;
      cout << "(r)eset. " << endl;
      cout << "(q)uit." << endl;
      cout << endl;
      cout << "Choose one : " ;
      cin >> choice ;

      if( !strchr("edurq",choice) && (choice>=97&&choice<=122) ){
         cout << "Enter e,d,u or q " << endl;}

      else if( !strchr("EDURQ",choice) && (choice<97&&choice>122) ){
         cout << "Enter E,D,U or Q " << endl;}

    }while( !strchr("edurqEDURQ",choice) );

 switch (choice) {
     case 'e' : enter(); break ;
     case 'd' : display(); break ;
     case 'u': update() ; break ;
     case 'r' : reset() ;break;
     case 'q' : return 0;
    }

  }
} 
A: 

This is a perfect time to use fall through in case statements.

switch (choice)
{
    case 'E':
    case 'e':
        enter();
        break;
    // etc.
}
Joe
+4  A: 

If you don't break for a case in a switch statement that matches it will continue on to the next one. If you put the capital cases before each lower case choice it will fall through.

switch (choice) {
     case 'E' :
     case 'e' : enter(); break ;
     case 'D' :
     case 'd' : display(); break ;
     case 'U' :
     case 'u': update() ; break ;
     case 'R' :
     case 'r' : reset() ;break;
     case 'Q' :
     case 'q' : return 0;
    }

The other option is to apply a string function to the user input to change it to lower case, in which case your existing switch statement would work.

RenderIn
This is silly because you need to type everything twice. Why not use tolower instead?
Frederik Slijkerman
He has pointed to that too.
rafael
+8  A: 

Use the tolower function to convert your input to lowercase and then you will only need to worry about the lowercase options.

Jeffrey Hines
A: 

Uppercase and lowercase characters use different character codes. So, if you use just lowercase cases in your switch, you will be testing just one type of cases.

You should either improve your switch statement tests, as other exemplified, or convert your choice character to lowercase. This way making sure you are providing the expected case for your switch tests.

fogo
A: 

Hack it

switch (choice | 0x20) {
    ...
wqw
Oh please... why don't you use a proper function instead :( ?
Matthieu M.
I'm "hacking" it :-))
wqw