What code should be written to accept the Lower and Upper case choices?

I start with C ++ and write a program that accepts the user's choice and acts in accordance with it ... my only problem is when the user enters the Uppercase option ... the program considers it to be the wrong choice ... for example, if "e" was the choice to enter the number. If the user entered "E", the program will not display the message "enter number". How can i fix this? I tried my best, but I can't get it to work. Oh, and how can I add uppercase cases in the switch? This is part of the code responsible for selecting the user and acting on 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;
    }

  }
} 
+3
5

switch, , . , .

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;
    }

- , , switch .

+5

tolower , .

+8

.

switch (choice)
{
    case 'E':
    case 'e':
        enter();
        break;
    // etc.
}
0

. , , .

You must either improve your tests of switch statements, as other examples, or convert your character choiceto lowercase. Thus, make sure that you provide the expected case for switch tests.

0
source

Hack it

switch (choice | 0x20) {
    ...
-2
source

Source: https://habr.com/ru/post/1733237/


All Articles