2个相同的代码,一个出错

2 identical codes, get error at one

我在代码中添加了注释,是否遇到了编译器问题?我想不通,我尝试查看 google 和这本书,但我无法弄清楚为什么前半部分代码只接受数字和单位之间带有 space 的输入,第二个代码接受数量和单位在一起。

我正在使用代码块。到目前为止,我尝试关闭它并再次打开它。

int main(){

    constexpr double dollar_to_euro = 0.91;
    constexpr double dollar_to_yen = 117.07;
    constexpr double dollar_to_pounds = 0.70;
    double sum = 1;

    char curr = '[=10=]'; // tried replacing '[=10=]' with '0' and ' ' 

    cout << "Please enter sum, followed by currency for conversion.\n"
         << "U for dollar, E for euro, Y for yen and P for pounds.\n";

    cin >> sum >> curr; // This is my issue, it does not want to accept "sumcurr" together, it only accepts it if theres space in between 
                        // yet on the second code for inches or centimeters it does accept them being together. Look down.
                        // For example entering "5 E" works, yet "5E" does not work. 
    if(curr=='E')
        cout << "The amount " << sum << " euro is " << sum/dollar_to_euro << " dollars\n";

    else
        cout << "GOD DAMMIT !!!!\n";


    constexpr double cm_per_inch = 2.54;

    double len = 1;

    char unit = '[=10=]';
    cout << "Please enter length followed by unit.\n";

    cin >> len >> unit; // Over here it works, this is an example from a book. Entering "5i" works. 

    if(unit=='i')
        cout << len << " in == " << cm_per_inch*len << "cm.\n";
    else
        cout << "Wrong input !\n";
}

这里的问题是 E/e 在浮点数中有效,但 5E/5e 不是您需要的有效浮点数E/e 之后的值。因此,当您输入 5e 时,sum 的输入失败,因为 5e0 有效的语法无效。如果您使用 E/e 以外的任何其他内容,那么它将像您的第二个示例一样工作。

有关浮点数格式的详细信息,请参阅:Cppreference floating point literal