【发布时间】:2021-01-22 06:24:10
【问题描述】:
#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是int,但您的switch案例是chars。使它们保持一致,即要么将n设为char,要么将switch case 设为整数。 -
case '1':->case 1:等等。 '1'的值取决于您平台上的字符编码。有趣的是,C++ 标准不允许为 1。 -
@Bathsheba 等一下,真的吗?我没有意识到这种限制。这是为什么?或者更确切地说,规则是什么?
-
@cigien: 0, 1, 2, ..., 9 在任何编码中都必须是连续的。
'0'不能为 0,因为后者是 NUL 终止符 - 用于表示 C 样式字符串的结束。因此,'1'不能为 1。
标签: c++ switch-statement