raf*_*ael 3 c++ lowercase uppercase
我是初学c ++并编写一个程序,接受用户选择并根据它行事......我唯一的问题是当用户输入大写选择时......程序将其视为错误的选择...就像' e'是输入数字的选择..如果用户输入'E',程序将不会显示"输入数字"消息.我可以修复它吗?我尽我所能,但我不能让它工作..哦,我怎么能在Switch案件中添加大写?这是代码的一部分,负责根据用户的选择和行动.
#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;
}
}
}
Run Code Online (Sandbox Code Playgroud)
如果你没有在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;
}
Run Code Online (Sandbox Code Playgroud)
另一个选项是将字符串函数应用于用户输入以将其更改为小写,在这种情况下,现有的switch语句将起作用.
| 归档时间: |
|
| 查看次数: |
8682 次 |
| 最近记录: |