【问题标题】:Conversion of 5 digit number into words using if else [duplicate]使用if else将5位数字转换为单词[重复]
【发布时间】:2019-04-02 08:47:42
【问题描述】:

我用 C++ 编写了一个程序来将数字显示为单词。该程序适用于 0-999 之间的数字,但不适用于 999 以上的数字。它为 999 以上的数字提供错误的输出。我不知道我必须在哪里进行一些更改才能使此代码正常工作。我已经用 dev c++ 编写了这个程序,而且我完全是 c++ 语言的初学者。

   //This program converts number into words between 0-99999
    #include<iostream>
    using namespace std;
    main()
    {
        long int number,unit,ten,hundred,thousand,ten_thousand;
        cout<<"Please enter any number between 0-99999: ";
        cin>>number;

        ten_thousand=number/10000;
        number=number%10000;

        thousand=number/1000;
        number=number%1000;

        hundred=number/100;
        number=number%100;

        ten=number/10;
        number=number%10;

        unit=number;

        if(number<0 || number>99999)
        {
            cout<<"Number is out of range"; return 0;
        }
        if(hundred>=1 && hundred <=9)
            {
            if(hundred==1) cout<<"one hundred";
            if(hundred==2) cout<<"two hundred";
            if(hundred==3) cout<<"three hundred";
            if(hundred==4) cout<<"four hundred";
            if(hundred==5) cout<<"five hundred";
            if(hundred==6) cout<<"six hundred";
            if(hundred==7) cout<<"seven hundred";
            if(hundred==8) cout<<"eight hundred";
            if(hundred==9) cout<<"nine hundred";
            }
        if(thousand>=1 && thousand <=9)
            {
            if(thousand==1) cout<<"one thousand";
            if(thousand==2) cout<<"one thousand";
            if(thousand==3) cout<<"one thousand";
            if(thousand==4) cout<<"one thousand";
            if(thousand==5) cout<<"one thousand";
            if(thousand==6) cout<<"one thousand";
            if(thousand==7) cout<<"one thousand";
            if(thousand==8) cout<<"one thousand";
            if(thousand==9) cout<<"one thousand";
            }
        if(ten_thousand >=1 && ten_thousand <=9)
            {
            if(ten_thousand==1) cout<<"one thousand";
            if(ten_thousand==2) cout<<"two thousand";
            if(ten_thousand==3) cout<<"three thousand";
            if(ten_thousand==4) cout<<"four thousand";
            if(ten_thousand==5) cout<<"five thousand";
            if(ten_thousand==6) cout<<"six thousand";
            if(ten_thousand==7) cout<<"seven thousand";
            if(ten_thousand==8) cout<<"eight thousand";
            if(ten_thousand==9) cout<<"nine thousand";
            }
        if(ten == 1)
        {
            if(number==10) cout<<"ten"; 
            if(number==11) cout<<"eleven"; 
            if(number==12) cout<<"twelve";
            if(number==13) cout<<"thirteen"; 
            if(number==14) cout<<"fourteen";
            if(number==15) cout<<"fifteen";
            if(number==16) cout<<"sixteen";
            if(number==17) cout<<"seventeen";
            if(number==18) cout<<"eighteen";
            if(number==19) cout<<"ninteen";
        }
        else {   
            if(ten==2) cout<<" twenty";
            if(ten==3) cout<<" thirty";
            if(ten==4) cout<<" fourty";
            if(ten==5) cout<<" fifty";
            if(ten==6) cout<<" sixty";
            if(ten==7) cout<<" seventy";
            if(ten==8) cout<<" eighty";
            if(ten==9) cout<<" ninty";

            if(unit==0 && ten ==0) cout<<" zero";
            if(unit==1) cout<<" one";
            if(unit==2) cout<<" two";
            if(unit==3) cout<<" three";
            if(unit==4) cout<<" four";
            if(unit==5) cout<<" five";
            if(unit==6) cout<<" six";
            if(unit==7) cout<<" seven";
            if(unit==8) cout<<" eight";
            if(unit==9) cout<<" nine";
        }
    }

输出:-

Please enter any number between 0-99999: 54587
five hundredone thousandfive thousand eighty seven

【问题讨论】:

  • thousand 的每个值的输出都相同
  • 你今天早些时候问了同样的问题。阅读那里的答案。
  • 这个问题和以前一样,他本来可以编辑问题的。显然,在这里,他缺少他在某些if 中添加的空格,而在其他空格中则没有。一些一致性会解决它。仍然是原始问题的副本。
  • " 这是一个学习使用 debugger 的好机会——你好 1 小时前"
  • @ThomasSablik 这两个问题都可以概括为“为什么它不起作用”。你真的认为每个错别字问一个问题好吗?

标签: c++ numbers display word


【解决方案1】:

在你的代码中

if(thousand>=1 && thousand <=9)
    {
    if(thousand==1) cout<<"one thousand";
    if(thousand==2) cout<<"one thousand";
    if(thousand==3) cout<<"one thousand";
    if(thousand==4) cout<<"one thousand";
    if(thousand==5) cout<<"one thousand";
    if(thousand==6) cout<<"one thousand";
    if(thousand==7) cout<<"one thousand";
    if(thousand==8) cout<<"one thousand";
    if(thousand==9) cout<<"one thousand";
    }

如您所见,您将为thousand 的所有值输出相同的字符串。改成

if(thousand>=1 && thousand <=9)
    {
    if(thousand==1) cout<<"one thousand";
    if(thousand==2) cout<<"two thousand";
    if(thousand==3) cout<<"three thousand";
    if(thousand==4) cout<<"four thousand";
    if(thousand==5) cout<<"five thousand";
    if(thousand==6) cout<<"six thousand";
    if(thousand==7) cout<<"seven thousand";
    if(thousand==8) cout<<"eight thousand";
    if(thousand==9) cout<<"nine thousand";
    }

【讨论】:

  • 或者所有的 if 没有正确排序?
  • 我已经解决了这个问题,并且我还对所有 if 语句进行了排序。该程序适用于所有值,除非我尝试输入 11-19 之间的数字或包含 11-19 的数字,但它给了我错误的输出。例如,请输入 0-99999 之间的任意数字:18231 108231
  • @Pixel666:您尝试调试您的代码吗?
  • 是的,我做到了,但我仍然找不到问题。
  • @Pixel666:第一个ten来自哪里?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-05-15
  • 2011-02-13
  • 2020-02-17
  • 1970-01-01
  • 2011-04-15
相关资源
最近更新 更多