私は現在C++でswitch文を使用していますが、いくつかの例を試してみました。私が選んだ例は、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;
}
€と£はおそらくUTF-8でロケール、内そうなマルチバイト文字です。したがって、それらを表すために単一の 'char'を使用することはできません。 –
デバッガでウォッチを開いて、実際にメモリ内にどのようなユニットがあるかを確認する必要があります。あなたは驚くかもしれません。 –
質問を編集して[mcve] – Slava