开关盒仅打印默认值

时间:2021-01-22 06:24:10

标签: c++ switch-statement

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

1 个答案:

答案 0 :(得分:8)

问题在于 n 是整数类型,但在 switch 语句中,您将其与 char 进行比较。

您可以使用这两个选项之一来修复它,

  1. n 更改为字符类型。
  2. 将比较更改为整数类型。

对于选项 1,这是一个简单的更改。

char n;

对于选项二,将 '1', '2', '3', ... 更改为 1, 2, 3, ... ..

switch (n) {
case 1:
    ...
    break;
case 2: 
    ...
    break;
...
}

确保在比较时不要特别混淆整数和字符!