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