-3

I have written a program in c++ to display numbers into words. The program is working fine for numbers between 0-999 but It is not working for numbers above 999. It is giving wrong output for numbers above 999. I don,t know where I have to makes some changes to get this code to work. I have written this program in dev c++ and I am a total beginner in c++ language.

   //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";
        }
    }

Output:-

Please enter any number between 0-99999: 54587
five hundredone thousandfive thousand eighty seven
Pixel666
  • 37
  • 2
  • 8
  • 1
    Your output is the same for each value of `thousand` – Thomas Sablik Oct 29 '18 at 12:09
  • You asked the same question earlier today. Read the answers there. – Matthieu Brucher Oct 29 '18 at 12:09
  • @MatthieuBrucher: Did you even read the question? It's not duplicate. That's two different questions for two different problems in the same code. – Thomas Sablik Oct 29 '18 at 12:10
  • 1
    It's the same question as before, and he could have edited the question in the first place. Obviously, here, he is missing spaces that he added in some `if` and not in others. Some consistency would solve it. Still a duplicate of the original question. – Matthieu Brucher Oct 29 '18 at 12:12
  • @MatthieuBrucher: In the linked question the problem was that OP forgot to remove the higher digits. In this question the problem is that OP calculates correct but the output is the same. No answer from linked question will help here. SO has the rule: only one question. – Thomas Sablik Oct 29 '18 at 12:14
  • 1
    *" This is a good opportunity to learn to use a [debugger](https://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems) – hellow 1 hour ago"* – hellow Oct 29 '18 at 12:16
  • @hellow: Of course OP should use the debugger but that doesn't mean that each question is a dup where OP didn't debug the code. – Thomas Sablik Oct 29 '18 at 12:18
  • 2
    @ThomasSablik both questions can be summarized as "why does it not work". Do you really think it is good to have one question per typo? – 463035818_is_not_an_ai Oct 29 '18 at 12:21
  • @user463035818: I think it is necessary to ask different questions for different problems as "Why does it not work" is not a appropriate question on SO. In the linked question the problem was a logical and mathematical problem. Here it is a copy and paste problem. – Thomas Sablik Oct 29 '18 at 12:23
  • *The program is working fine for numbers between 0-999 but It is not working for numbers above 999* -- If that's the case, then this is a good place to start learning how to write functions instead of sticking the entire program inside of `main()`. Write a function that converts a 3 digit number to words, and just repeatedly call that function, with the only difference being sticking the word "million", "thousand", etc. in-between the return values. So a simple rearrangement of your code is all that's required, that is -- **if** the conversion of a 3 digit number is working correctly. – PaulMcKenzie Oct 29 '18 at 12:43

1 Answers1

-1

In your code you have

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";
    }

As you can see you will output the same string for all values of thousand. Change it to

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";
    }
Thomas Sablik
  • 16,127
  • 7
  • 34
  • 62