开关盒仅打印默认值
#include<iostream>
#include<algorithm>
using namespace std;
int main(){
int n;
//only printing the default value
cout<<"Press 1 for Hindi, Press 2 for English, Press 3 for Punjabi, ess 4 for Japanese."<<endl;
cin>>n;
switch (n)
{
case '1':
cout<<"Namaste";
break;
case '2':
cout<<"Hello";
break;
case '3':
cout<<"Sat Shri Akal";
case '4':
cout<<"Ohaio gosaimas";
break;
default:
cout<<"I am still learing more!!!";
}
return 0;
}
回答
问题是这n是一个整数类型,但在 switch 语句中,你将它与chars进行比较。
您可以使用这两个选项之一来修复它,
- 更改
n为字符类型。 - 将比较更改为整数类型。
对于选项 1,它是一个简单的更改。
char n;
对于选项二,将'1', '2', '3', ...更改为1, 2, 3, ...
switch (n) {
case 1:
...
break;
case 2:
...
break;
...
}
确保在比较时不要特别混淆整数和字符!