Pixel666
Pixel666

Reputation: 37

Converting two number digit into words

I have written a program in c++ to convert number into words. The output for 0-19 is fine but it is giving wrong output for numbers between 20-99.

//This program converts numbers from 0-99 into words
#include<iostream>
using namespace std;
int main()
{
    int number,unit,ten;
    cout<<"Please enter any number between 0-99: ";
    cin >>number;
    ten=number/10;
    unit=number%10;
    if(number<0 | number>99)
        cout<<"Number is out of range";
    if(number>=11 & number <=19)
      {
        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(unit==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";
        if(ten==10) cout<<"Ten";
        if(ten==20) cout<<"twenty";
        if(ten==30) cout<<"thirty";
        if(ten==40) cout<<"fourty";
        if(ten==50) cout<<"fifty";
        if(ten==60) cout<<"sixty";
        if(ten==70) cout<<"seventy";
        if(ten==80) cout<<"eighty";
        if(ten==90) cout<<"ninty";
    }               
}   

Output:

Please enter any number between 0-99: 25
five

Upvotes: 2

Views: 77

Answers (2)

mss
mss

Reputation: 1493

There are only two places where you need to just do minor changes and you will get you desired result

first when you are separing the ten part, multiply it with 10 aftwards to checking for multiple of 10 in your if conditions of multiple of 10 i.e. 10 , 20 ,.. and then you need to just change the position of all the ten position comparison to above all other one's digit number comparisons

I copied your code and did changes, I have commented below where you need to change

    //This program converts numbers from 0-99 into words
    #include<iostream>
    using namespace std;
    int main()
    {   
        int number,unit,ten;
        cout<<"Please enter any number between 0-99: ";
        cin >>number;
        ten=number/10;
        unit=number%10;
        ten=ten*10;// multiply again it make it power of 10 so that you can check for if for multiple of 10
        if(number<0 | number>99)
            cout<<"Number is out of range";
        if(number>=11 & number <=19)
          {
            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==10) cout<<"Ten";
            if(ten==20) cout<<"twenty";
            if(ten==30) cout<<"thirty";
            if(ten==40) cout<<"fourty";
            if(ten==50) cout<<"fifty";
            if(ten==60) cout<<"sixty";
            if(ten==70) cout<<"seventy";
            if(ten==80) cout<<"eighty";
            if(ten==90) cout<<"ninty";
            //all ten position comparisons has been shifted before all one's position comparisons
            if(unit==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";    }               
    }   

Upvotes: 0

Rarblack
Rarblack

Reputation: 4664

Actually, your program is working correctly. For input 25 it should give five because you are doing unit = number%10 which returns 5 and if(unit==5) cout<<"five"; right from your own code.

If you want to get twenty five in return you should change the code.

Instead of making direct matching, store numbers in map and check the value of it. and another approch is to store them in 2 different lists like: d1 = ['zero','one' ...] and d10 = ['ten', 'twenty', 'thirty', ...]. By this, you will not repeat anything. Furthermore, you can check the length of the input and know if it one decimal number or two and if 2 decimal number you can get the first one with number/10 and the second one with number%10 and concatenate the result. Let' take 25 you will search 2 in the d10 list and 5 in the d1 list and at the end, you end up 'twenty' + 'five'

n1 = number/10
n2 = number%10
for i in d10:
    if i == n1:
        for j in d1:
            if j == n2:
                print(i+' ' +j)
            else:
               break
     else:
         break

NOTE: the code is in python 3.x

Upvotes: 1

Related Questions