C ++ iostream中的奇怪BUG? [英] Strange BUG in C++ iostream?

查看:67
本文介绍了C ++ iostream中的奇怪BUG?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是iostream中的错误吗? ...

is this a bug in 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;

    }

当键盘输入为

  • '5p'或

  • '5p' or

'5 p'或

'3y'或

'3 y'或

'1 z'或

1z'或

'10 e'

一切都按预期进行.

当输入为'(anyting)e'时,它是一个错误(例如'5e') 我已经尝试了几乎所有方法来使它起作用,但没有成功.

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.

当我删除while()循环时,输入'5e'->时得到此输出 "Sorry, unknown valute ( )",但是当我输入时说'7m'我得到输出"Sorry, unknown valute (m)."

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)."

我做了很多事情,因为在大代码中,这可能是一个几乎无法察觉的错误. 在某些情况下,'e'作为char输入是否有问题?

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?

推荐答案

不,这不是不是 C ++流类中的错误.

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

您需要将输入内容输入为std::string并自己提取值和货币.

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

这是因为e用于以科学计数法分隔有效数指数,这是指定double.因此10e是无效的double,因为它缺少了定义指数的部分.

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.

顺便说一句,使用GBP,EUR和JPY(这是您要支持的货币的ISO代码)可以减少特质.

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

这篇关于C ++ iostream中的奇怪BUG?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆