简体   繁体   English

C ++ iostream中的奇怪BUG?

[英]Strange BUG in C++ iostream?

is this a bug in iostream? 这是iostream中的一个错误吗? ... ...

        #include<iostream>
        void money_conversion (){

        constexpr double dollars_in_yen=0.01;
        constexpr double dollars_in_euro=1.16;
        constexpr double dollars_in_pound=1.33;
        std::cout<<"Supported valutes : yen ('y'), euros('e'), pounds('p').\n";
        std::cout<<"Please enter the value + valute that you want to convert into dollars! :";
        double value=1;
        char valute=0;
        while(true){
        std::cin>>value>>valute;
        if(valute=='y')
            std::cout<<"\n\n"<<value<<" yens is "<<value*dollars_in_yen<<" dollars. \n";
        else if(valute=='e')
            std::cout<<"\n\n"<<value<<" euros is "<<value*dollars_in_euro<<" dollars. \n";
        else if(valute=='p')
            std::cout<<"\n\n"<<value<<" pounds is "<<value*dollars_in_pound<<" dollars. \n";
        else
            std::cout<<"\n\nSorry, unknown valute ("<<valute<<").\n";
        }

    }


    int main(){

        money_conversion();
        return 0;

    }

When keyboard input is 键盘输入时

  • '5p' or '5p'或

  • '5 p' or '5 p'或

  • '3y' or '3y'或

  • '3 y' or '3年'或

  • '1 z' or '1 z'或

  • 1z' or 1z'或

  • '10 e' '10 e'

everything goes as expected. 一切都按预期进行。

When the input is '(anyting)e' its an error (like '5e' ) I have tryied pretty much everything to try to make it work but no success. 当输入是'(anyting)e'它出现错误(比如'5e' )我已经尝试了很多东西来试图让它工作但没有成功。

When i remove while() loop i get this output when entered '5e' -> "Sorry, unknown valute ( )" but when i enter lets say '7m' i get output "Sorry, unknown valute (m)." 当我删除while()循环时,我输入'5e'时得到此输出 - > "Sorry, unknown valute ( )"但是当我输入时说'7m'我得到输出"Sorry, unknown valute (m)."

I make of this a big deal because, in large code, this can be an error that is almost impossible to notice. 我认为这是一个大问题,因为在大代码中,这可能是一个几乎不可能注意到的错误。 Is 'e' a problem as char input in some cases? 在某些情况下, 'e'char输入的问题吗?

When a std::istream or std::locale library function attempts to parse any numeric input, it always first grabs all contiguous characters in the set "0123456789abcdefxABCDEFX+-" and which might be valid for the type of conversion being done, and only then tries to determine what they mean. std::istreamstd::locale库函数尝试解析任何数字输入时,它总是先捕获集合"0123456789abcdefxABCDEFX+-"中的所有连续字符,这些字符可能对正在进行的转换类型有效,然后才会试图确定他们的意思。 See the description of Stage 2 of the num_get processing . 请参阅num_get处理第2阶段的说明。

So in your "5e" example, the operator>>(double&) function grabs both '5' and 'e' , expecting to find an exponent after the 'e' , but stops there, and those characters don't make a valid complete double . 所以在你的"5e"例子中, operator>>(double&)函数同时捕获'5''e' ,期望在'e'之后找到一个指数,但是在那里停止,并且那些字符不能生效完全double

No, this is not a bug in the C++ stream classes. 不,这不是 C ++流类中的错误。

You need to read in the input as a std::string and extract the value and the currency yourself. 您需要将输入读入为std::string并自行提取值和货币。

That's because e is used to separate the significand and the exponent in scientific notation , which is another way of specifying a double . 这是因为e用于以科学记数法分隔有效数指数 ,这是指定double精度的另一种方式。 Threfore 10e is an invalid double as it's missing the portion that defines the exponent. 因为它缺少定义指数的部分,因此10e是无效的double精度。

By the way, using GBP, EUR, and JPY (which are the ISO codes for the currencies you want to support) would be less idiosyncratic. 顺便说一下,使用GBP,EUR和JPY(这是您想要支持的货币的ISO代码)将不那么特殊。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM