C++ c++;是否将6e(或任何带e的数字)解释为输入?

C++ c++;是否将6e(或任何带e的数字)解释为输入?,c++,C++,当运行此代码并输入6e或带有e的任何其他数字时,程序无法识别它,并最终进入else状态。我做错了什么 #include "../std_lib_facilities.h" int main() { cout << "Please enter amount of money followed by the currency name:" + "y for yen, p for pound or e for e

当运行此代码并输入6e或带有e的任何其他数字时,程序无法识别它,并最终进入else状态。我做错了什么

#include "../std_lib_facilities.h"

int main() {
    cout << "Please enter amount of money followed by the currency name:" 
        + "y for yen, p for pound or e for euro, like 3.40e. \n"
        << "This app will convert it to dollars. \n";
    double amount;
    char currency = ' ';
    double dollars;
    cin >> amount >> currency;

    cout<<"amount:  "<< amount <<"\n";
    cout<<"currency :" << currency<<"\n";

    if (currency == 'y'){
        cout <<amount<<"Yuan = "<< amount * 0.15<<" dollars";
    }
    else if (currency == 'e') {
        cout << amount << "Euro = " << amount * 1.18 << " dollars";
    }
    else if (currency == 'p') {
        cout << amount << "Pounds = " << amount * 1.29<< " dollars";
    }
    else {
        dollars = 0;
        cout << "Unknown currency\n";
    }
}
#包括“./std_lib_facilities.h”
int main(){
金额>>货币;
cout读取由空格分隔的元素(或直到出现不可解析的字符)

6e
不能被解析为
double
(但是
e
是科学记数法的有效字符,因此它被使用),并且
cin
进入失败状态,并且随后的每个
调用都失败

对于
cin>>amount>>currency;
要工作,输入应该像
6e
(或
6e0e

如果希望
6e
也作为输入,则需要将其作为字符串读取(或用读取整行),然后自己解析(例如,提取和后缀字符,然后将剩余数字解析为
double

另请参见此类似问题:.

读取由空格分隔的元素(或直到出现不可解析的字符)

6e
不能被解析为
double
(但是
e
是科学记数法的有效字符,因此它被使用),并且
cin
进入失败状态,并且随后的每个
调用都失败

对于
cin>>amount>>currency;
要工作,输入应该像
6e
(或
6e0e

如果希望
6e
也作为输入,则需要将其作为字符串读取(或用读取整行),然后自己解析(例如,提取和后缀字符,然后将剩余数字解析为
double


另请参见此类似问题:.

请注意
6f
表示最接近6的浮点值。因此,我建议读取和验证用户输入字符串。请注意
6f
表示最接近6的浮点值。因此,我建议读取和验证用户输入字符串。