Thread: (Not a repeat post) - Implement the source code that turns numbers into English text

  1. #1
    Registered User
    Join Date
    Jun 2012
    Location
    Morden, UK
    Posts
    128

    (Not a repeat post) - Implement the source code that turns numbers into English text

    Hi everyone,

    I have been working on the same problem as mp252 from an earlier thread and 4 days later, some hair pulling, a bit of lost sleep, and now, a big sigh of relief, I appear to have it working. Mine only goes from 0-9999 though as I must move on!

    Code:
    #include <iostream>
    #include <string>
    
    int getThousands(int number);
    int getHundreds(int number);
    int getTens(int number);
    int getUnits(int number);
    
    void printNumber(int number);
    
    std::string tens[10] = {"", "Ten", "Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety"};
    std::string units[10] = {"", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine"};
    std::string specialCase[10] = {"", "Eleven",
        "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen"};
    
    int main()
    {
        int number;
        
        do
        {
            std::cout << "Please enter a number between 1 - 9999: ";
            std::cin >> number;
            
            if (number > 0 && number < 10000)
            {
                printNumber(number);
            }
            else
            {
                std::cout << "Number too big! Try again." << std::endl;
            }
            
        } while (number > 9999);
    }
    
    int getThousands(int number)
    {
        int noOfDivides = number/1000;
        return noOfDivides;
    }
    
    int getHundreds(int number)
    {
        int noOfDivides = (number % 1000)/100;
        return noOfDivides;
    }
    
    int getTens(int number)
    {
        int noOfDivides = (number % 100)/10;
        return noOfDivides;
    }
    
    int getUnits(int number)
    {
        int remainder = number % 10;
        return remainder;
    }
    
    void printNumber(int number)
    {
        if (!(getThousands(number) == 0)) /*if getThousands returns something
                                           other than 1, it's a 4 digit number*/
        {
            std::cout << units[getThousands(number)] << " Thousand ";
        }
        
        if (!(getHundreds(number) == 0)) /*if getHundreds returns something
                                          other than 1, it's a 3 digit number*/
        {
            std::cout << units[getHundreds(number)] << " Hundred ";
        }
       
        
        if (!(getTens(number) < 2)) /* (419%100)/10 = 1.9 = 1. (420%100)/10 = 2. Only
                                     want this block to run if last 2 digits are 20 or above*/
        {
            std::cout << tens[getTens(number)] << " ";
        }
        
        if (number < 20) /* i.e. number is between 0 - 19, just take value and use it
                          straight in the array to get text version*/
        {
            std::cout << units[number];
        }
        else if (getTens(number) == 1) /*if final 2 digits are between 11-19 i.e. 119, 417, use the specialCase array for 'teens'*/
        {
            std::cout << specialCase[getUnits(number)]; /*for when 3 and 4 digit numbers
                                                         end in 11-19*/
        }
        else
        {
            std::cout << units[getUnits(number)];
        }
    }
    I didn't want to look at the other solution because that wouldn't help me.

    I had a scrap of paper like a mad scientist trying to find relationships between numbers and the values that they would return through my functions. This is how I arrived at the conditions of my if statements in 'void printNumber'.

    I have seen other code that allows a greater range but I can't quite follow it (yet):

    C++ code by fun2code - 67 lines - codepad

    I'm sure this could be achieved in another way though, any suggestions for improvement to my version are appreciated. This is my first time with arrays and the starting at [0] bit was slightly confusing. My functions could be better named, and I'm sure there are many other things!

    I wont keep posting my programs all the time unless I have a specific problem, but I'm not that confident at the moment and your evaluations are very useful to me. Thanks.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    But once you got to 1000, the rest is very easy.

    123,123,123 is
    one hundred and twenty three million, one hundred and twenty three thousand, one hundred and twenty three.

    It's just /1000 and %1000 to get groups of 3 digits, and then append the appropriate suffix.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 15
    Last Post: 04-17-2013, 09:00 AM
  2. Replies: 6
    Last Post: 08-20-2012, 07:09 AM
  3. To DOnt repeat the same numbers ... (ARRAYS)
    By haiderali in forum C Programming
    Replies: 3
    Last Post: 01-29-2012, 11:34 AM
  4. generate non repeat numbers
    By abyss in forum C Programming
    Replies: 20
    Last Post: 10-27-2009, 09:51 AM
  5. detecting repeat numbers
    By volk in forum C++ Programming
    Replies: 4
    Last Post: 04-20-2003, 12:19 PM