Like Tree2Likes
  • 1 Post By Salem
  • 1 Post By grumpy

Enormous number-to-text program!

This is a discussion on Enormous number-to-text program! within the C++ Programming forums, part of the General Programming Boards category; I've reached a point in "Jumping into C++" where I need to make a program that converts input numbers into ...

  1. #1
    Registered User
    Join Date
    Nov 2013
    Posts
    14

    Enormous number-to-text program!

    I've reached a point in "Jumping into C++" where I need to make a program that converts input numbers into their word equivalent.

    So far I've made it work for numbers 0-9999. I've tried implementing 10000-99999 but there are problems with the order of the words printed (57865 would print fifty thousand seven thousand eight hundred sixty five). But besides that, the program is absolutely enormous (for me) and I'm wondering if it can be shortened. Keep in mind I can only use loops and if statements so far. Here it is:

    Code:
    #include <iostream>
    #include <string>
    
    
    void extras(int e);
    void digits(int x);
    void tens(int xx);
    void hundreds(int xxx);
    void thousands(int xxxx);
    //void tens_of_thousands(long xxxxx);
    
    
    using namespace std;
    
    
        string zero="zero";
        string one="one";
        string two="two";
        string three="three";
        string four="four";
        string five="five";
        string six="six";
        string seven="seven";
        string eight="eight";
        string nine="nine";
        string ten="ten";
        string eleven="eleven";
        string twelve="twelve";
        string thirteen="thirteen";
        string fourteen="fourteen";
        string fifteen="fifteen";
        string sixteen="sixteen";
        string seventeen="seventeen";
        string eighteen="eighteen";
        string nineteen="nineteen";
        string twenty="twenty";
        string thirty="thirty";
        string forty="forty";
        string fifty="fifty";
        string sixty="sixty";
        string seventy="seventy";
        string eighty="eighty";
        string ninety="ninety";
        string hundred="hundred";
        string thousand="thousand";
    
    
    int main()
    {
        long number;
        cout<<"Enter number: ";
        cin>>number;
        if(number<0)
        {
            cout<<"You cannot enter a negative number. Try again"<<endl;
            cin>>number;
        }
        long aux=number;
        int i=0;
        if(number>20)
        {
            while(aux!=0)
        {
            i++;
            aux=aux/10;
        }
        if(i==1)
        {
            digits(number);
        }
        else if(i==2)
        {
            tens(number);
        }
        else if(i==3)
        {
            hundreds(number);
        }
        else if(i==4)
        {
            thousands(number);
        }
       /* else if(i==5)
        {
            tens_of_thousands(number);
        } */
        }
        else
        {
            extras(number);
        }
    
    
        cin.ignore();
        cin.get();
    }
    
    
    
    
    void extras(int e)
    {
        if(e==0)
        {
            cout<<zero;
        }
        else if(e==1)
        {
            cout<<one;
        }
        else if(e==2)
        {
            cout<<two;
        }
        else if(e==3)
        {
            cout<<three;
        }
        else if(e==4)
        {
            cout<<four;
        }
        else if(e==5)
        {
            cout<<five;
        }
        else if(e==6)
        {
            cout<<six;
        }
        else if(e==7)
        {
            cout<<seven;
        }
        else if(e==8)
        {
            cout<<eight;
        }
        else if(e==9)
        {
            cout<<nine;
        }
        else if(e==10)
        {
            cout<<ten;
        }
        else if(e==11)
        {
            cout<<eleven;
        }
        else if(e==12)
        {
            cout<<twelve;
        }
        else if(e==13)
        {
            cout<<thirteen;
        }
        else if(e==14)
        {
            cout<<fourteen;
        }
        else if(e==15)
        {
            cout<<fifteen;
        }
        else if(e==16)
        {
            cout<<sixteen;
        }
        else if(e==17)
        {
            cout<<seventeen;
        }
        else if(e==18)
        {
            cout<<eighteen;
        }
        else if(e==19)
        {
            cout<<nineteen;
        }
        else
        {
            cout<<twenty;
        }
    }
    
    
    void digits(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;
        }
    }
    
    
    
    
    void tens(int xx)
    {
        if(xx<20)
        {
            extras(xx);
        }
        else if(xx/10==2)
        {
            cout<<twenty<<" ";
            digits(xx%10);
        }
        else if(xx/10==3)
        {
            cout<<thirty<<" ";
            digits(xx%10);
        }
        else if(xx/10==4)
        {
            cout<<forty<<" ";
            digits(xx%10);
        }
        else if(xx/10==5)
        {
            cout<<fifty<<" ";
            digits(xx%10);
        }
        else if(xx/10==6)
        {
            cout<<sixty<<" ";
            digits(xx%10);
        }
        else if(xx/10==7)
        {
            cout<<seventy<<" ";
            digits(xx%10);
        }
        else if(xx/10==8)
        {
            cout<<eighty<<" ";
            digits(xx%10);
        }
        else if(xx/10==9)
        {
            cout<<ninety<<" ";
            digits(xx%10);
        }
    }
    
    
    
    
    void hundreds(int xxx)
    {
        if(xxx/100==1)
        {
            cout<<one<<" "<<hundred<<" ";
            tens(xxx%100);
        }
        else if(xxx/100==2)
        {
            cout<<two<<" "<<hundred<<" ";
            tens(xxx%100);
        }
        else if(xxx/100==3)
        {
            cout<<three<<" "<<hundred<<" ";
            tens(xxx%100);
        }
        else if(xxx/100==4)
        {
            cout<<four<<" "<<hundred<<" ";
            tens(xxx%100);
        }
        else if(xxx/100==5)
        {
            cout<<five<<" "<<hundred<<" ";
            tens(xxx%100);
        }
        else if(xxx/100==6)
        {
            cout<<six<<" "<<hundred<<" ";
            tens(xxx%100);
        }
        else if(xxx/100==7)
        {
            cout<<seven<<" "<<hundred<<" ";
            tens(xxx%100);
        }
        else if(xxx/100==8)
        {
            cout<<eight<<" "<<hundred<<" ";
            tens(xxx%100);
        }
        else if(xxx/100==9)
        {
            cout<<nine<<" "<<hundred<<" ";
            tens(xxx%100);
        }
    }
    
    
    
    
    void thousands(int xxxx)
    {
        if(xxxx/1000==1)
        {
            cout<<one<<" "<<thousand<<" ";
            hundreds(xxxx%1000);
        }
        else if(xxxx/1000==2)
        {
            cout<<two<<" "<<thousand<<" ";
            hundreds(xxxx%1000);
        }
        else if(xxxx/1000==3)
        {
            cout<<three<<" "<<thousand<<" ";
            hundreds(xxxx%1000);
        }
        else if(xxxx/1000==4)
        {
            cout<<four<<" "<<thousand<<" ";
            hundreds(xxxx%1000);
        }
        else if(xxxx/1000==5)
        {
            cout<<five<<" "<<thousand<<" ";
            hundreds(xxxx%1000);
        }
        else if(xxxx/1000==6)
        {
            cout<<six<<" "<<thousand<<" ";
            hundreds(xxxx%1000);
        }
        else if(xxxx/1000==7)
        {
            cout<<seven<<" "<<thousand<<" ";
            hundreds(xxxx%1000);
        }
        else if(xxxx/1000==8)
        {
            cout<<eight<<" "<<thousand<<" ";
            hundreds(xxxx%1000);
        }
        else if(xxxx/1000==9)
        {
            cout<<nine<<" "<<thousand<<" ";
            hundreds(xxxx%1000);
        }
    }
    
    
    /*
    void tens_of_thousands(long xxxxx)
    {
            if(xxxxx/10000==1)
        {
            if(xxxxx%1000<1000)
            {
                cout<<ten<<" "<<thousand<<" ";
                hundreds(xxxxx%1000);
                cout<<" ";
            }
            else
            {
                cout<<ten<<" "<<thousand<<" ";
                thousands(xxxxx%10000);
            }
        }
        else if(xxxxx/10000==2)
        {
            cout<<twenty<<" "<<thousand<<" ";
            thousands(xxxxx%10000);
        }
        else if(xxxxx/10000==3)
        {
            cout<<thirty<<" "<<thousand<<" ";
            thousands(xxxxx%10000);
        }
        else if(xxxxx/10000==4)
        {
            cout<<forty<<" "<<thousand<<" ";
            thousands(xxxxx%10000);
        }
        else if(xxxxx/10000==5)
        {
            cout<<fifty<<" "<<thousand<<" ";
            thousands(xxxxx%10000);
        }
        else if(xxxxx/10000==6)
        {
            cout<<sixty<<" "<<thousand<<" ";
            thousands(xxxxx%10000);
        }
        else if(xxxxx/10000==7)
        {
            cout<<seventy<<" "<<thousand<<" ";
            thousands(xxxxx%10000);
        }
        else if(xxxxx/10000==8)
        {
            cout<<eighty<<" "<<thousand<<" ";
            thousands(xxxxx%10000);
        }
        else if(xxxxx/10000==9)
        {
            cout<<ninety<<" "<<thousand<<" ";
            thousands(xxxxx%10000);
        }
    
    
    
    
    }
    */

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,452
    Have you learned how to use arrays?
    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

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Do you know about arrays? That might help.

    You should be able to reuse the [print one-to-twenty] to do [print one-to-twenty thousand].

  4. #4
    Registered User
    Join Date
    Nov 2013
    Posts
    14
    Unfortunately, Allex sure likes to torture newbies in his book. Arrays are a couple of chapters ahead, so I can only assume I was meant to do this practice problem with the resources at hand. Like I said, just loops and ifs.....

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,452
    You shouldn't have a function called thousands at all.

    Consider
    123,123,123
    Is
    One hundred and twenty three million, one hundred and twenty three thousand, one hundred and twenty three.

    Also, consider using arrays.
    Code:
    string units[] = { "zero", "one", "two", "three", ... };
    Then you can simply do
    cout << units[ num % 10 ];
    And save yourself a long if / else chain.
    whiteflags likes this.
    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.

  6. #6
    Registered User
    Join Date
    Nov 2013
    Posts
    14
    I think I'll just wait and see what I can do with arrays. Thanks guys.

  7. #7
    Registered User
    Join Date
    Jun 2005
    Posts
    6,208
    You might also wish to write functions that returns the strings you need, rather than printing out strings.

    Once you have the strings assembled (remember strings can be appended to each other) then print them. That saves you needing to sprinkle use of strings like zero, one, two, etc everywhere in your code, and means you only need one or two output statements. And would probably help reduce the size of your code by a lot.

    There is a basic rule: if you want your code to be simple, do simple things, and don't do too many at once. Writing functions that unnecessarily do multiple things (working out what strings to output and then outputting them are two things, and can be done separately) makes your code more convoluted than necessary.

    Your code also has a lot of duplication.
    Last edited by grumpy; 11-16-2013 at 06:00 PM.
    whiteflags likes this.
    Right 98% of the time, and don't care about the other 3%.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Number to Text translation
    By GrapesofWonder in forum C Programming
    Replies: 6
    Last Post: 02-18-2013, 11:44 PM
  2. Replies: 4
    Last Post: 10-15-2012, 03:26 AM
  3. Printing text to a number of pages
    By new_in_c++ in forum Windows Programming
    Replies: 2
    Last Post: 07-11-2011, 08:58 PM
  4. Input a Hex number and output hex number to a text field
    By zoobaby in forum C++ Programming
    Replies: 4
    Last Post: 05-12-2009, 11:26 AM
  5. Enormous Input Test
    By samus250 in forum C++ Programming
    Replies: 51
    Last Post: 01-03-2009, 11:12 AM

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