Numbers to words

This is a discussion on Numbers to words within the C++ Programming forums, part of the General Programming Boards category; So i'm currently reading the website host book, and one of the situations we are addressed with is converting numbers ...

  1. #1
    Registered User
    Join Date
    Dec 2012
    Posts
    1

    Numbers to words

    So i'm currently reading the website host book, and one of the situations we are addressed with is converting numbers into their text, e.g. 103; one hundred three. Well i was able to finally get all my code to work. But i'm wondering if i didn't go about this very efficiently can somebody look over my code and tell me? Also I'm not using pointers yet i think that might have made this easier but not too sure.

    Code:
    #include <iostream>
    
    using namespace std;
    
    
    int tens(int remainder);
    int hundreds(int x);
    int thousands(int x);
    void numtoword(int x);
    void hardcode(int x);
    
    
    
    
    
    
    
    
    
    
    int main()
    {
        int x;
        cout<<"What number do you want to convert to text?\n";
        cin>>x;
        numtoword(x);
    
    
        return 0;
    }
    
    
    
    
    
    
    
    
    void numtoword(int x)
    {
        if (x < 20){
            hardcode(x);
        }
        else if (x > 99 && x < 1000){
            hundreds(x);
        }
        else if(x > 999 && x < 1000000){
            thousands(x);
        }
    
    
    
    
    }
    
    
    
    
    void hardcode(int x)
    {
        if(x == 1){
            cout<<" One ";
        }
        else if(x == 2){
            cout<<" Two ";
    }
        else if(x == 3){
            cout<<" Three ";
        }
        else if(x == 4){
            cout<<" Four ";
        }
        else if(x == 5){
            cout<<" Five ";
        }
        else if(x == 6){
            cout<<" Six ";
        }
        else if(x == 7){
            cout<<" Seven ";
        }
        else if(x == 8){
            cout<<" Eight ";
        }
        else if(x == 9){
            cout<<" Nine ";
        }
        else if(x == 10){
            cout<<" Ten";
        }
        else if(x == 11){
            cout<<" Eleven";
        }
        else if(x == 12){
            cout<<" Tweleve";
        }
        else if(x == 13){
            cout<<" Thirteen";
        }
        else if(x == 14){
            cout<<" Fourteen";
        }
        else if(x == 15){
            cout<<" Fifteen";
        }
        else if(x == 16){
            cout<<" Sixteen";
        }
        else if(x == 17){
            cout<<" Seventeen";
        }
        else if(x == 18){
            cout<<" Eighteen";
        }
        else if(x == 19){
            cout<<" Nineteen";
        }
    }
    
    
    
    
    int thousands(int x)
    {
            int firstnum = x/1000;
            int bignum = x % 1000;
            while(x > 1000){
            x = x - 1000;
            }
            if (bignum < 100 && firstnum < 20){
                hardcode(firstnum);
                cout<<" Thousand ";
                tens(bignum);
    
    
            }
            else if(firstnum < 20  && bignum > 99)
            {
                hardcode(firstnum);
                cout<<" Thousand ";
                hundreds(bignum);
            }
            else if(firstnum > 19 && firstnum < 100)
            {
                tens(firstnum);
                cout<<" Thousand ";
                hundreds(bignum);
            }
            else if(firstnum > 99)
            {
                hundreds(firstnum);
                cout<< " Thousand ";
                hundreds(x);
    
    
            }
    
    
    
    
    
    
    }
    
    
    int hundreds(int x)
    {
        if(x > 99){
                if(x < 200){
                int number = x / 100;
                int remainder = (x - 100);
                hardcode(number);
                cout<<" Hundred ";
                tens(remainder);
                }
                else if(x < 300){
                    int number = x / 100;
                    int remainder = (x - 200);
                    hardcode(number);
                    cout<<" Hundred ";
                    tens(remainder);
                }
                else if(x < 400){
                    int number = x / 100;
                    int remainder = (x - 300);
                    hardcode(number);
                    cout<<" Hundred ";
                    tens(remainder);
                }
                else if(x < 500){
                    int number = x / 100;
                    int remainder = (x - 400);
                    hardcode(number);
                    cout<<" Hundred ";
                    tens(remainder);
                }
                else if(x < 600){
                    int number = x / 100;
                    int remainder = (x - 500);
                    hardcode(number);
                    cout<<" Hundred ";
                    tens(remainder);
                }
                else if(x < 700){
                    int number = x / 100;
                    int remainder = (x - 600);
                    hardcode(number);
                    cout<<" Hundred ";
                    tens(remainder);
                }
                else if(x < 800 || x == 800){
                    int number = x / 100;
                    int remainder = (x - 700);
                    hardcode(number);
                    cout<<" Hundred ";
                    tens(remainder);
                }
                else if(x < 900){
                    int number = x / 100;
                    int remainder = (x - 800);
                    hardcode(number);
                    cout<<" Hundred ";
                    tens(remainder);
                }
                else if(x < 1000){
                    int number = x / 100;
                    int remainder = (x - 900);
                    hardcode(number);
                    cout<<" Hundred ";
                    tens(remainder);
                }
            }
        return 0;
    }
    
    
    int tens(int remainder)
    {
        if(remainder > 19 && remainder < 30){
            cout<<" Twenty";
            int digi = remainder - 20;
            hardcode(digi);
        }
        else if(remainder > 29 && remainder < 40){
            cout<<" Thirty";
            int digi = remainder - 30;
            hardcode(digi);
        }
        else if(remainder > 39 && remainder < 50){
            cout<< " Fourty";
            int digi = remainder - 40;
            hardcode(digi);
        }
        else if(remainder > 49 && remainder < 60){
            cout<< " Fifty";
            int digi = remainder - 50;
            hardcode(digi);
        }
        else if(remainder > 59 && remainder< 70){
            cout<<" Sixty";
            int digi = remainder - 60;
            hardcode(digi);
        }
        else if(remainder > 69 && remainder < 80){
            cout<<" Seventy";
            int digi = remainder - 70;
            hardcode(digi);
        }
        else if(remainder > 79 && remainder < 90){
            cout<<" Eighty";
            int digi = remainder - 80;
            hardcode(digi);
        }
        else if(remainder > 89 && remainder < 100){
            cout<<" Ninety";
            int digi = remainder - 90;
            hardcode(digi);
        }
        else{
            hardcode(remainder);
        }
        return 0;
    }

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,422
    > int number = x / 100;
    > int remainder = (x - 100);
    Look up what the % operator does.

    And also consider something like this
    Code:
    char *strings[] = { "", "one", "two", "three", "four" };
    cout << strings[num];
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,675
    Quote Originally Posted by Salem View Post
    And also consider something like this
    Code:
    char *strings[] = { "", "one", "two", "three", "four" };
    cout << strings[num];
    Hool learn how to use the C++ string class now. For sure you will need it in the past
    (Of course what Salem says it's correct )
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  4. #4
    Registered User
    Join Date
    Apr 2006
    Posts
    2,020
    Salem's suggestion is good, but I would make it a static const char *[].
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  5. #5
    Registered User
    Join Date
    Jun 2005
    Posts
    6,177
    Apart from the suggestions above, the word "twelve" only contains two letter 'e's.
    Right 98% of the time, and don't care about the other 3%.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Numbers to words
    By Dontgiveup in forum C++ Programming
    Replies: 28
    Last Post: 03-25-2011, 06:54 AM
  2. Replies: 5
    Last Post: 12-21-2007, 12:38 PM
  3. Numbers To words
    By Sesshokotsu in forum C Programming
    Replies: 3
    Last Post: 10-05-2006, 05:30 PM
  4. Numbers In Words
    By year2038bug in forum C Programming
    Replies: 2
    Last Post: 09-08-2005, 10:21 AM
  5. converting numbers into words
    By Kozam in forum C Programming
    Replies: 2
    Last Post: 09-30-2003, 07:49 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21