作为课程的一部分,我已经用C++编写了一个简单的程序来在货币之间进行转换。它要求一个数值,然后是一个字母(y,e或p)来表示其中一种支持的货币。当使用'y'或'p'时,您可以将数值和字符一起输入或用空格分隔(例如:“100y”或“100y”),它可以正常工作。但是,仅字母'e',如果我输入在一起,它不会识别为有效的输入。有谁知道为什么?在C++中的货币之间进行转换的程序中的问题
下面的代码:
#include <iostream>
int main()
{
using namespace std;
constexpr double yen_to_dollar = 0.0081; // number of yens in a dollar
constexpr double euro_to_dollar = 1.09; // number of euros in a dollar
constexpr double pound_to_dollar = 1.54; // number of pounds in a dollar
double money = 0; // amount of money on target currency
char currency = 0;
cout << "Please enter a quantity followed by a 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;
}
原因是'100y'不是有效数字的开始,而是'100e' - 以科学记数法。 – Angew
请勿对货币使用浮点数。人们对奇币 –