首页 > 解决方案 > c++ 如何将 6e(或任何带有 e 的数字)解释为输入?

问题描述

当运行此代码并使用 e 输入 6e 或任何其他数字时,程序无法识别它并最终进入 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";
    }
}

标签: c++

解决方案



推荐阅读