-10

I am a beginner and I have written a program in c++ to convert numbers between 0-99999 into words. It is working fine for numbers upto 100 but after that it gives wrong output. I know there are some serious logical errors but I am just unable to figure these out.

//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;
    thousand=number/1000;
    hundred=number/100;
    ten=number/10;
    unit=number%10;

    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 1:- (Correct)

Please enter any number between 0-99999: 85
eighty five

Output 2:- (Wrong)

Please enter any number between 0-99999: 254
two hundred four

Output 3:- (Wrong)

Please enter any number between 0-99999: 98541
nine thousand one
Pixel666
  • 37
  • 2
  • 8
  • 4
    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 Oct 29 '18 at 10:23
  • 2
    you are not picking the digits (i guess thats what you wanted to do): try to calculate `hundred=number/100;` for `number = 1234` by hand – 463035818_is_not_an_ai Oct 29 '18 at 10:27
  • related, not dup: https://stackoverflow.com/q/53040738/5470596 – YSC Oct 29 '18 at 10:46
  • 1
    Apart from everything that's been said here: this is what a debugger is for. If you knew how to use your debugger to run your program one line at a time, and examine the values of all variables as they change, you would've quickly figured out this bug all by yourself, and you wouldn't have to ask for help on some web site. So, going forward, looks like you have two options: 1) Learn how to use a debugger, to quickly debug your own code 2) Post a question on stackoverflow.com every time your program doesn't work correctly. Which option makes the most sense to you? – Sam Varshavchik Oct 29 '18 at 10:51
  • The point of this exercise is for you to see all that tedious similarity and think ”perhaps there’s a pattern and I could use a loop and maybe a lookup table”. – molbdnilo Oct 29 '18 at 11:02

4 Answers4

3

When you select the units for say thousands, you don't remove the unit before. You forgot the modulo:

thousand=(number % 10000)/1000;

The same is true for all your units, except the first one and the last one, and this is why you only display the first non-zero number and then the last one.

Matthieu Brucher
  • 21,634
  • 7
  • 38
  • 62
1

Assume that you want to run your code with 945.

The first calculation would be

945/100 = 9

If you perform number/10 this will lead to:

945/10 = 94

That is why your code does not work. Every time that you divide your "number" you have to scale it.

number = 945
hund = 945/100 = 9
number = number - hund*100 = 45
ten = number/10 = 45/10 ( 4 )
number = number - ten*10 = 5
units = number/1 = 5
1

Your extraction logic is incorrect, as any good debugger would have revealed to you.

Consider something of the form

unit = number % 10;
number /= 10; // remove the least significant digit by integer division
ten = number % 10;
number /= 10;
hundred = number % 10;
number /= 10;

and so on. This approach is nice as it can be converted to a loop at a later time, with unit, ten, hundred, &c. eventually becoming elements of an array.

Also don't forget to extract any negative sign.

Bathsheba
  • 231,907
  • 34
  • 361
  • 483
0

Try this :

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;
Vishal Srivastav
  • 563
  • 6
  • 13