简体   繁体   English

C ++ Switch语句和符号

[英]C++ Switch statement and symbols

I'm currently using switch statements with c++, I worked through some examples and used an independent example to finish with. 我目前在c ++中使用switch语句,我研究了一些示例,并使用了一个独立的示例作为结尾。 The example I chose was a currency converter from eur to gbp. 我选择的示例是从欧元到GBP的货币转换器。 In all cases, my code reverted to the default as if the wrong unit was input. 在所有情况下,我的代码都恢复为默认值,就像输入了错误的单位一样。 This happened until I changed my input from the '€ symbol or '£' symbol to 'E' or 'P'. 直到我将输入从“€”符号或“£”符号更改为“ E”或“ P”之前,这种情况一直发生。 I'd like to know where I went wrong. 我想知道我哪里做错了。 an example of my code is below. 下面是我的代码示例。

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

£ is a unicode symbol, and therefore does not fit into an 8-bit char . £是unicode符号,因此不适合8位char Your compiler hopefully gave a warning like warning: multi-character character constant for the line in question. 希望您的编译器发出类似warning: multi-character character constant该行的warning: multi-character character constant

You should be able to fix this with: 您应该可以通过以下方法解决此问题:

#include <cwchar>

...

wchar_t x = u'£';

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM