Like Tree3Likes

Implement the source code that turns numbers into English text

This is a discussion on Implement the source code that turns numbers into English text within the C++ Programming forums, part of the General Programming Boards category; Implement the source code that turns numbers into English text for numbers between -2 billion and 2 billion. this is ...

  1. #1
    Registered User
    Join Date
    Mar 2013
    Posts
    36

    Implement the source code that turns numbers into English text

    Implement the source code that turns numbers into English text for numbers between -2 billion
    and 2 billion.
    this is off the ebook im having trouble with this chapter
    the algorithm is supposed to look like this
    1) Break the number up into chunks of three digits
    2) For each three-digit chunk, compute the text; append the magnitude of that chunk; append the
    chunks together
    3) To compute the text of a three-digit chunk, compute the number of hundreds, and convert that
    one-digit number to text, and add “hundreds”, appending the text of the two-digit chunk
    4) To compute the text of a two-digit chunk, if it’s less than 20, look it up; if it’s greater than 20,
    compute the number of tens, and look up the word, and append the text of the one-digit
    number

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    1,620
    Ok, but what's the problem?
    Elkvis and stahta01 like this.
    Devoted my life to programming...

  3. #3
    Registered User
    Join Date
    Oct 2006
    Posts
    2,300
    you need to show us that you at least tried to solve this problem.

  4. #4
    Registered User
    Join Date
    Mar 2013
    Posts
    36
    Hi I haven't thats why im getting confused. I dont want you to show me source code. I just need a bit more explanation if that is ok?

  5. #5
    Registered User
    Join Date
    Jun 2011
    Posts
    2,241
    Break the problem down into small manageable steps. Tackle them one at a time.

    A development process

    You already have a basic algorithm. Start writing code for #1 in your OP. If you get stuck there, show what you have and ask specific questions.

  6. #6
    Registered User
    Join Date
    Mar 2013
    Posts
    36
    thanks will give it a try.

  7. #7
    Registered User
    Join Date
    Mar 2013
    Posts
    36
    ok i have written this so far I need to write another function to call the words probably using a string array. Is this the right route to follow?

    Code:
    #include <iostream> //including header files
    #include <string>
    
    
    using namespace std;
    
    
    
    
    
    
    int main() //main body of fcn
    {
      int num; //declaring integers
      int hundreds;
      int thousands;
      int millions;
      int billions;
    
    
      cout << "Enter an a number between -2 billion and 2 billion " << endl; //prompts user for i/p and stores it
      cin >> num;
    
    
        if ((num < -2000000000) || (num > 2000000000)) //exits program if not within these limits
        {
            return 0;
        }
    
    
        if ((num >= -2000000000) || (num <= 2000000000)) //if within these limits  execute the statement
        {
           hundreds = ((num % 1000)/100); //work out how many hundreds
           cout << hundreds << endl;
    
    
           thousands = ((num % 100000)/1000); //work out how many thousdands
           cout << thousands << endl;
    
    
           millions = ((num % 10000000)/100000); //work out how many millions
           cout << millions << endl;
    
    
           billions = ((num % 1000000000)/1000000); //work out how many billions
           cout << billions << endl;
    
    
        }
    
    
    
    
    }

  8. #8
    Registered User
    Join Date
    Jun 2011
    Posts
    2,241
    Not a bad start. Note that you check the input value on line 25, and exit if it's out of range. Therefore, there is no need to check the number again as you do on line 31.

    Also, I believe that "int" is not guaranteed to hold 4 bytes (for a range of ~ +/-2 billion), so you might want to consider using "long" instead.

    Variables. Data Types. - C++ Documentation

    Code:
    Enter an a number between -2 billion and 2 billion
    1987654321
    3
    54
    76
    987
    
    Process returned 0 (0x0)   execution time : 7.768 s
    Press any key to continue.
    Code:
    Enter an a number between -2 billion and 2 billion
    1234
    2
    1
    0
    0
    
    Process returned 0 (0x0)   execution time : 1.856 s
    Press any key to continue.
    Did you run and test this? The results are not quite right, so you might want to play around with the logic before moving to the next step. (See how useful the gradual approach to programming is?)

    FYI: I'm a 'C' programmer, and have little 'C++' experience, but the help you need seems to be more logic-based than syntax-based, which gives me more flexibility in giving you assistance.
    stahta01 likes this.

  9. #9
    Registered User
    Join Date
    Mar 2013
    Posts
    36
    thanks again i see where i have gone wrong. I shall proceed further! Also the gradual approach is much better ; )

  10. #10
    Registered User
    Join Date
    Mar 2013
    Posts
    36
    hi again! I have managed to sort the code out a lot. Doing bit by bit really helps. However I have got stuck on how to print 10000, 11000, etc onto 100000. I can do one or the other it seems. I have two functions (func5 and func6) that are doing it in my code below but want to try and fit it into one is this possible? I stopped here as I'm guessing I would run into the same problem when I reach higher numbers. Also another silly question (I am a noob!) is that I somehow got my de-bugger to go into its own window and and cannot get it back. I have tried to look in the view but it says it is still there I am using code blocks version 10.05. thanks again
    Code:
     #include <iostream> //including header files#include <string>
    
    
    using namespace std;
    
    
    //string array declared
    string units[3] [10] = {{"","One","Two","Three", "Four", "Five", "Six","Seven","Eight", "Nine"},{"Eleven", "Twelve", "Thirteen","Fourteen", "Fifteen", "Sixteen", "Seventeen","Eighteen", "Nineteen"},{"Ten", "Twenty", "Thirty", "Fourty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety"}}; 
    
    
    //function defs
    void func6  (int unsigned long i);
    void func5 (int unsigned long i);
    void func4 (int unsigned i);
    void func3 (int unsigned i);
    void func2 (int unsigned i);
    void func1 (int unsigned t);
    void func (int unsigned i);
    
    
    
    
    
    
    int main() //main body of fcn
    {
    long int num; //declaring integers
    
    
    int j=1;
    
    
      while (j==1) //start of while loop
      {
    
    
    
    
            cout << "Enter a number between -2 billion and 2 billion " << endl; //prompts user for i/p and stores it
            cin >> num;
    
    
    
    
            if ((num < -2000000000) || (num > 2000000000)) //exits program if not within these limits
            {
                cout << "Number Invalid " << endl;
                return 0;
            }
            else
            {
                func(num);  //call functions
                func1(num);
                func2(num);
                func3(num);
                func4(num);
                func5(num);
                func6(num);
            }
        }
    }
    
    
    void func6 (int unsigned long i) //start of fcn5
    {
        int unsigned long t; //declares integer t
    
    
        if (i>9999&&i<100000) //if within these limits statement executes
        {
            t=i%1000; //works out remainder
            i=i/1000; //works out new i value
            cout << units[1][i-11] << " Thousand "; //prints out result whilst looking up table
    
    
            if (t<10) //if remainder within limits calls function
            func(t);
            else if (t>=10&&t<20)
            func1(t);
            else if (t>19&&t<=100)
            func2(t);
            else if (t>99&&t<1000)
            func3(t);
    
    
        }
    }
    
    
    
    
    void func5 (int unsigned long i) //start of fcn5
    {
        int unsigned long t; //declaring integer
        if (i>9999&&i<100000) // if value within these limits then execute
        {
            t=i%10000; //works out remainder
            i=i/10000; //works out new value of i
            cout << units[2][i-1] << " Thousand "; //prints out result whilst looking up table
    
    
            if (t<10) //calls function if remainder within these limits
            func(t);
            else if (t>=10&&t<20)
            func1(t);
            else if (t>=20&&t<100)
            func2(t);
            else if (t>=100&&t<1000)
            func3(t);
      
        }
    }
    
    
    
    
    
    
    void func4 (int unsigned i) //start of function 4
    {
        int unsigned t; //declares integer
        if (i>=1000&&i<10000) //if within these limits execute fcn
        {
            t=i%1000; //works out remainder
            i=i/1000; //works out new value of i
            cout << units[0][i] << " Thousand "; //prints out result whilst looking up table
            
            if (t<10) //calls function if remainder within limits
            func(t);
            else if (t>=10&&t<20)
            func1(t);
            else if (t>=20&&t<100)
            func2(t);
            else if (t>=100&&t<1000)
            func3(t);
    
    
        }
    }
    
    
    void func3 (int unsigned i) //start of fcn 3
    {
        int unsigned t; //delcares integer
        if (i>=100&&i<1000) //statement executes if within limits
        {
            t=i%100; //works out remainder 
            i=i/100; //works out new value
            cout << units[0][i] << " Hundred "; //prints out result whilst looking up table
            
            if (t<10)   //if remainder within these limits calls previous functions
            func(t);
            else if (t>=10&&t<20)
            func1(t);
            else if (t>=20&&t<100)
            func2(t);
        }
    }
    
    
    void func2(int unsigned i) //start of fcn2
    { 
        int unsigned t; //declares integer
    
    
        if (i>=20&&i<100) //if i within limits statement executes
        {
                t=i%10; //works out remainder
                i=i/10; //works out new i value
                cout << units[2][i-1] ; //prints result out whilst looking up table
    
    
                if (t < 10) //if remainder within these limits calls function
                func(t);
        }
    }
    
    
    void func1(int unsigned i) //start of func1
    {
        int unsigned t; //declares integer
        if (i>=10&&i<20) //if i within these limits statement executes
        {
            t=i%10; //works out remainder
            cout << units[1][t-1] << endl; //prints out value whilst looking up table
        }
    }
    
    
    void func(int unsigned t) //start of fcn 
    {
        if (t<10) //if statement less than 10 executes
        {
            cout << " " << units[0][t] << endl; //prints out result whilst looking up table.
        }
    }

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,448
    Instead of writing the initialiser for your array on one line, break it up across several lines to make it more readable.

    Instead of giving your functions names like func6, func5, etc, give them descriptive names.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  12. #12
    Registered User
    Join Date
    Mar 2013
    Posts
    36
    ok duely noted will do in future. Any suggestions on my problem?

  13. #13
    Registered User
    Join Date
    May 2009
    Posts
    2,516
    Quote Originally Posted by mp252 View Post
    Also another silly question (I am a noob!) is that I somehow got my de-bugger to go into its own window and and cannot get it back. I have tried to look in the view but it says it is still there I am using code blocks version 10.05. thanks again
    This is a guess on the CB issue,
    Try "View" -> "Perspectives" -> "Delete Current"
    This normally deletes the CB window layout and restores it to a default.

    Once, you get it the way you want.
    I suggest "View" -> "Perspectives" -> "Save Current" and give it a unique name.

    Edit: Your coding problem is NOT worth my time until you re-post your code with real descriptive function names!

    Tim S.
    "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to produce bigger and better idiots. So far, the Universe is winning." Rick Cook

  14. #14
    Registered User
    Join Date
    Mar 2013
    Posts
    36
    Here is my code with the rectified function names. The problem that I am having is with the printTenThousands function. At the moment it is printing the value from the look up table however it is ignoring the remainder, I think I know the solution to this. The other problem I keep running into is with printing 20000, 30000 ....90000. This is to do with the i value. I'm not sure how to go about this.

    Code:
    #include <iostream> //including header files
    #include <string>
    
    
    using namespace std;
    
    
    //string array declared
    string units [10] = {"One","Two","Three", "Four", "Five", "Six","Seven","Eight", "Nine"};
    string teens [10] = {"Ten", "Eleven", "Twelve", "Thirteen","Fourteen", "Fifteen", "Sixteen", "Seventeen","Eighteen", "Nineteen"};
    string tens [10] = {"Twenty", "Thirty", "Fourty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety"};
    
    
    //function defs
    void printTenThousands (int unsigned long i);
    void printThousands (int unsigned i);
    void printHundreds(int unsigned i);
    void printTens(int unsigned i);
    void printTeens(int unsigned t);
    void printUnits (int unsigned i);
    
    
    int main() //main body of fcn
    {
    long int num; //declaring integers
    
    
    int j=1;
    
    
      while (j==1) //start of while loop
      {
            cout << "Enter a number between -2 billion and 2 billion " << endl; //prompts user for i/p and stores it
            cin >> num;
    
    
            if ((num < -2000000000) || (num > 2000000000)) //exits program if not within these limits
            {
                cout << "Number Invalid " << endl;
                return 0;
            }
            else
            {
                printUnits(num);  //call functions
                printTeens(num);
                printTens(num);
                printHundreds(num);
                printThousands(num);
                printTenThousands(num);
            }
        }
    }
    
    
    void printTenThousands (int unsigned long i) //start of fcn to print hundred thousands
    {
        int unsigned long t; //declares integer t
    
    
        if (i>9999&&i<100000) //if within these limits statement executes
        {
            t=i%1000;//works out remainder
            t=t*1000;
            i=i/1000; //works out new i value
            cout << teens[i-10] << " Thousand "; //prints out result whilst looking up table
            
            if (t<10) //if remainder within limits calls function
            printUnits(t);
            else if (t>9&&t<20)
            printTeens(t);
            else if (t>19&&t<100)
            printTens(t);
            else if (t>99&&t<1000)
            printHundreds(t);
    
    
        }
    }
    
    
    void printThousands (int unsigned i) //start of func to print thousands
    {
        int unsigned t; //declares integer
        if (i>999&&i<10000) //if within these limits execute fcn
        {
            t=i%1000; //works out remainder
            i=i/1000; //works out new value of i
            cout << units[i-1] << " Thousand "; //prints out result whilst looking up table
    
    
            if (t<10) //calls function if remainder within limits
            printUnits(t);
            else if (t>9&&t<20)
            printTeens(t);
            else if (t>=20&&t<100)
            printTens(t);
            else if (t>99&&t<1000)
            printHundreds(t);
    
    
        }
    }
    
    
    void printHundreds (int unsigned i) //start of fcn to print hundreds
    {
        int unsigned t; //delcares integer
        if (i>99&&i<1000) //statement executes if within limits
        {
            t=i%100; //works out remainder
            i=i/100; //works out new value
            cout << units[i-1] << " Hundred "; //prints out result whilst looking up table
    
    
            if (t<10)   //if remainder within these limits calls previous functions
            printUnits(t);
            else if (t>9&&t<20)
            printTeens(t);
            else if (t>=20&&t<100)
            printTens(t);
        }
    }
    
    
    void printTens (int unsigned i) //start of fcn to print tens
    {
        int unsigned t; //declares integer
    
    
        if (i>19&&i<100) //if i within limits statement executes
        {
                t=i%10; //works out remainder
                i=i/10; //works out new i value
                cout << tens[i-2] ; //prints result out whilst looking up table
    
    
                if (t<10) //if remainder within these limits calls function
                printUnits(t);
        }
    }
    
    
    void printTeens (int unsigned i) //start of func to print teens
    {
        int unsigned t; //declares integer
        if (i>9&&i<=19) //if i within these limits statement executes
        {
            t=i%10; //works out remainder
            cout << teens[t-1] << endl; //prints out value whilst looking up table
        }
    }
    
    
    void printUnits (int unsigned t) //start of fcn to print units
    {
        if (t<10) //if statement less than 11 executes
        {
            cout << " " << units[t-1] << endl; //prints out result whilst looking up table.
        }
    }

  15. #15
    Registered User
    Join Date
    May 2009
    Posts
    2,516
    Quote Originally Posted by mp252 View Post
    1) Break the number up into chunks of three digits
    Please point out in the code where you are doing this step.

    Edit: I do NOT think you are doing that step.

    I suggest doing that step and storing the values into variables or an array.

    Edit2: After doing that print out the value with commas in the number.

    Input: 1234567890
    Output: 1,234,567,890

    Edit3: Then try to print out the numbers as words.
    Output2: One Billion Two Hundred Thirty-four million five hundred sixty seven thousand and eight hundred ninety

    Tim S.
    Last edited by stahta01; 04-16-2013 at 07:31 PM.
    "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to produce bigger and better idiots. So far, the Universe is winning." Rick Cook

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Alternating turns in C code for 2 player game
    By richardpd in forum C Programming
    Replies: 7
    Last Post: 01-20-2013, 04:41 PM
  2. Replies: 6
    Last Post: 08-20-2012, 07:09 AM
  3. Extract text(English & Chinese) from pdf
    By wonderway in forum C Programming
    Replies: 1
    Last Post: 03-31-2011, 11:39 PM
  4. C++ program help(converting numbers to english words)
    By bama1 in forum C++ Programming
    Replies: 4
    Last Post: 10-08-2002, 01:17 AM
  5. How to implement several source files?
    By Gades in forum C Programming
    Replies: 3
    Last Post: 11-21-2001, 01:44 PM

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