I did one of the try this exercises in Stroustrup's PPP 2nd Edition and the program is supposed to accept a value followed by a suffix indicating the currency. This should be converted to dollars. The following code works for "15y" or "5p", but when I enter "6e" it gives me "Unknown currency".
constexpr double yen_per_dollar = 124.34;
constexpr double euro_per_dollar = 0.91;
constexpr double pound_per_dollar = 0.64;
/*
The program accepts xy as its input where
x is the amount and y is its currency
it converts this to dollars.
*/
double amount = 0;
char currency = 0;
cout << "Please enter an amount to be converted to USD\n"
<< "followed by its currency (y for yen, e for euro, p for pound):\n";
cin >> amount >> currency;
if (currency == 'y') // yen
cout << amount << currency << " == "
<< amount/yen_per_dollar << " USD.\n";
else if (currency == 'e') // euro
cout << amount << currency << " == "
<< amount/euro_per_dollar << " USD.\n";
else if (currency == 'p') // pound
cout << amount << currency << " == "
<< amount/pound_per_dollar << " USD.\n";
else
cout << "Unknown currency.\n";
If I type "6 e" instead it works fine but I don't understand why the others work even without space.