Strange BUG in C++ iostream?

前端 未结 2 1044
予麋鹿
予麋鹿 2021-01-22 16:26

is this a bug in iostream? ...

        #include
        void money_conversion (){

        constexpr double dollars_in_yen=0.01;
        constexp         


        
相关标签:
2条回答
  • 2021-01-22 16:56

    No, this is not a bug in the C++ stream classes.

    You need to read in the input as a std::string and extract the value and the currency yourself.

    That's because e is used to separate the significand and the exponent in scientific notation, which is another way of specifying a double. Threfore 10e is an invalid double as it's missing the portion that defines the exponent.

    By the way, using GBP, EUR, and JPY (which are the ISO codes for the currencies you want to support) would be less idiosyncratic.

    0 讨论(0)
  • 2021-01-22 17:10

    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. See the description of Stage 2 of the num_get processing.

    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.

    0 讨论(0)
提交回复
热议问题