0

I'm currently using switch statements with c++, I worked through some examples and used an independent example to finish with. The example I chose was a currency converter from eur to 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'. 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;
}
user0042
  • 7,917
  • 3
  • 24
  • 39

1 Answers1

0

£ is a unicode symbol, and therefore does not fit into an 8-bit char. Your compiler hopefully gave a warning like warning: multi-character character constant for the line in question.

You should be able to fix this with:

#include <cwchar>

...

wchar_t x = u'£';
0x5453
  • 12,753
  • 1
  • 32
  • 61