C++ Switch语句和符号
问题描述:
我正在使用switch语句和C++,我通过一些示例并使用了一个独立的示例来完成。我选择的例子是从eur到gbp的货币转换器。在所有情况下,我的代码都会恢复为默认值,就像输入了错误的单位一样。发生这种情况,直到我将输入从'€符号或'£'符号更改为'E'或'P'。我想知道我哪里错了。我的代码示例如下。C++ Switch语句和符号
// Currency: Convert GBP to EUR or Vice Versa
int main(){
double gbp_to_eur = 1.09;
double eur_to_gbp = 0.92;
char unit = '£';
double amount_to_convert = 0;
int AnyKey = 0;
cout << "Please enter the the unit you'd like to convert \n";
cin >> unit;
cout << "\n \n Now please enter the amount you'd like to convert. \n";
cin >> amount_to_convert;
switch (unit) {
case 'P':
cout << "Your " << unit << amount_to_convert << " is worth €" << amount_to_convert * gbp_to_eur << '.\n';
break;
case 'E':
cout << "Your " << unit << amount_to_convert << " is worth €" << amount_to_convert * eur_to_gbp << '.\n';
break;
default: cout << "The compiler isn't programmed for this unit of currency. \n";
break;
}
cin >> AnyKey;
}
答
£
是Unicode符号,并且因此不适合于一个8位char
。您的编译器希望为该行提供warning: multi-character character constant
的警告。
你应该能够与解决此问题:
#include <cwchar>
...
wchar_t x = u'£';
+1
8位字符,而不是字节:) –
+0
@MichaelDorgan哈哈,谢谢。 – 0x5453
€和£在你的语言环境,这可能是UTF-8可能多字节字符。因此,你不能用一个'char'来表示它们。 –
您应该在调试器中打开一块手表,并查看内存中的实际外观。你可能会感到惊讶。 –
请编辑您的问题包含[mcve] – Slava