if statement - Trouble in a program to convert between currencies in C++ -
i've made simple program in c++ convert between currencies, part of lesson. asks numerical value , letter (y, e or p) represent 1 of supported currencies. when using 'y' or 'p' can input numerical value , character or separated space (ie: "100y" or "100 y") , it'll work fine. however, letter 'e' only, if enter both together, doesn't recognize valid input. have idea why?
here's code:
#include <iostream> int main() { using namespace std; constexpr double yen_to_dollar = 0.0081; // number of yens in dollar constexpr double euro_to_dollar = 1.09; // number of euros in dollar constexpr double pound_to_dollar = 1.54; // number of pounds in dollar double money = 0; // amount of money on target currency char currency = 0; cout << "please enter quantity followed currency (y, e or p): " << endl; cin >> money >> currency; if(currency == 'y') cout << money << "yen == " << yen_to_dollar*money << "dollars." << endl; else if(currency == 'e') cout << money << "euros == " << money*euro_to_dollar << "dollars." << endl; else if(currency == 'p') cout << money << "pounds == " << money*pound_to_dollar << "dollars." << endl; else cout << "sorry, currency " << currency << " not supported." << endl; return 0; }
when enter 100e10e works ok. 100e10 valid number in scientific notation. 100e not valid number in scientific notation. not converted double , money assigned 0. variable currency stays unchanged. why "sorry, currency not supported" message. e belongs number in case, because fits scientific notation format.
you assign 4 chars every currency( _eur instance ) . solve problem , more user friendly.
Comments
Post a Comment