2017-08-24 10 views
0

私は現在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; 
} 
+3

€と£はおそらくUTF-8でロケール、内そうなマルチバイト文字です。したがって、それらを表すために単一の 'char'を使用することはできません。 –

+1

デバッガでウォッチを開いて、実際にメモリ内にどのようなユニットがあるかを確認する必要があります。あなたは驚くかもしれません。 –

+1

質問を編集して[mcve] – Slava

答えて

0

£ Unicodeのシンボルであり、したがって、8ビットcharに適合しません。あなたのコンパイラは問題の行についてwarning: multi-character character constantのような警告をうまく与えました。

あなたがしてこの問題を解決することができるはずです。

#include <cwchar> 

... 

wchar_t x = u'£'; 
+1

の8ビット文字(バイトではありません)を入力してください。 –

+0

@MichaelDorgan Haha、ありがとうございます。 – 0x5453

関連する問題