Reputation: 676
I am making a simple currency conversion program using a switch statement as shown below:
#include "library/std_lib_facilities.h"
int main()
{//This program converts yen, euros, yuan, kroner and pounds to dollars
double amount = 0;
char currency = ' ';
//one dollar equivalent of each currency
const double yen_to_dollar = 113.67;
const double pounds_to_dollar = 0.85;
const double euros_to_dollar = 0.95;
const double yuan_to_dollar = 6.87;
const double kroner_to_dollar = 7.04;
//case labels corresponding to currency
const char y = 'y', p = 'p', e = 'e', u = 'u', k = 'k';
cout << "Please type the amount you want to convert, followed by the currency(y,e,p,u,k) u is for yuan: ";
while(cin >> amount >> currency ) {
switch(currency) {
case y:
cout << amount << " yen == " << amount / yen_to_dollar << " dollars." << '\n';
break;
case p:
cout << amount << " pounds == " << amount / pounds_to_dollar << " dollars." <<'\n';
break;
case e:
cout << amount << " euros == " << amount / euros_to_dollar << " dollars." << '\n';
break;
case u:
cout << amount << " yuan == " << amount / yuan_to_dollar << " dollars." << '\n';
break;
case k:
cout << amount << " kroner == " << amount / kroner_to_dollar << " dollars." << '\n';
break;
default:
cout << "Please try supported currencies" << '\n';
break;
}
}
return 0;
}
I can convert the predefined currencies to dollars when the function is run by inputting amount and currency, like this: 5y (converts 5 yen to the dollar equivalent).
Every other amount and constant work EXCEPT the e
constant. The code ends anytime I want to test it with an amount and constant e.g. (24e).
When I change the constant e = 'e'
to something like s = 's'
, it works very well.
So my question is why would the input containing an e character break my code?
Upvotes: 0
Views: 58
Reputation: 34217
This might NOT be it but is the entered "23.4e" value being interpreted as an exponent? That would be my thought here. You might consider parsing it removing the string/char part first into your variable for that part.
Upvotes: 3