Thread: QUESTION: Convert numbers to words

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    2

    QUESTION: Convert numbers to words

    Create a program that prompts the user to enter a number (0-100000 only) then output the number in words.

    Enter a number: 10012
    it outputs " ten thousand two" instead of ten thousand twelve...
    can you please tell me what are my errors ?

    im also having a problem with the 11-19 ten thousandths case ...

    here's my program...

    Code:
    #include <iostream>
    
    
    using namespace std;
    
    
    void main()
    {
    
    
        int num,ones,tenths,hundredths,thousandths,tenthousandths,hundredthousandths;
     
        cout<<"Enter a Number: ";
        cin>>num;
     
        if (num==0)
            cout<<"zero";
        if (num>=0&&num<=100000)
        {
            hundredthousandths=num/100000;
    
    
            switch (hundredthousandths)
            {
            case 1:
                cout<<" one hundred thousand ";
                break;
            }
    
    
            tenthousandths=num/10000;
    
    
            switch (tenthousandths)
            {
            case 0:
                cout<<" ";
                break;
            case 1:
                cout<<"ten";
                break;
            case 2:
                cout<<"twenty";
                break;
            case 3:
                cout<<"thirty";
                break;
            case 4:
                cout<<"forty";
                break;
            case 5:
                cout<<"fifty";
                break;
            case 6:
                cout<<"sixty";
                break;
            case 7:
                cout<<"seventy";
                break;
            case 8:
                cout<<"eighty";
                break;
            case 9:
                cout<<"ninety";
                break;
            }
    
    
            thousandths=num/1000;
    
    
            switch (thousandths)
            {
            case 0:
                cout<<" thousand ";
                break;
            case 1:
                cout<<" one thousand ";
                break;
            case 2:
                cout<<" two thousand ";
                break;
            case 3:
                cout<<" three thousand ";
                break;
            case 4:
                cout<<" four thousand ";
                break;
            case 5:
                cout<<" five thousand ";
                break;
            case 6:
                cout<<" six thousand ";
                break;
            case 7:
                cout<<" seven thousand ";
                break;
            case 8:
                cout<<" eight thousand ";
                break;
            case 9:
                cout<<" nine thousand ";
                break;
            }
            
            
            hundredths=num/100;
    
    
            switch (hundredths)
            {
            case 1:
                cout<<"one hundred ";
                break;
            case 2:
                cout<<"two hundred ";
                break;
            case 3:
                cout<<"three hundred ";
                break;
            case 4:
                cout<<"four hundred ";
                break;
            case 5:
                cout<<"five hundred ";
                break;
            case 6:
                cout<<"six hundred ";
                break;
            case 7:
                cout<<"seven hundred ";
                break;
            case 8:
                cout<<"eight hundred ";
                break;
            case 9:
                cout<<"nine hundred ";
                break;
            }
    
    
            
            tenths=num/10;
    
    
            switch (tenths)
            {
            case 2:
                cout<<"twenty ";
                break;
            case 3:
                cout<<"thirty ";
                break;
            case 4:
                cout<<"forty ";
                break;
            case 5:
                cout<<"fifty ";
                break;
            case 6:
                cout<<"sixty ";
                break;
            case 7:
                cout<<"seventy ";
                break;
            case 8:
                cout<<"eighty ";
                break;
            case 9:
                cout<<"ninety ";
                break;
    
    
            }
            
            ones=num;
            if(ones>=1&&ones<=19)
            
            {
            switch (ones)
            {
                case 0:
                    cout<<" ";
                    break;
                case 1:
                    cout<<"one";
                    break;
                case 2:
                    cout<<"two";
                    break;
                case 3:
                    cout<<"three";
                    break;
                case 4:
                    cout<<"four";
                    break;
                case 5:
                    cout<<"five";
                    break;
                case 6:
                    cout<<"six";
                    break;
                case 7:
                    cout<<"seven";
                    break;
                case 8:
                    cout<<"eight";
                    break;
                case 9:
                    cout<<"nine";
                    break;
                case 10:
                    cout <<"Ten";
                    break;
                case 11:
                    cout <<"Eleven";
                    break;
                case 12:
                    cout <<"Twelve";
                    break;
                case 13:
                    cout <<"Thirteen";
                    break;
                case 14:
                    cout <<"Fourteen";
                    break;
                case 15:
                    cout <<"Fifteen";
                    break;
                case 16:
                    cout <<"Sixteen";
                    break;
                case 17:
                    cout <<"Seventeen";
                    break;
                case 18:
                    cout <<"Eighteen";
                    break;
                case 19:
                    cout <<"Nineteen";
                    break;
                default:
                    cout <<" Error ";
            }
        }
        cout<<endl;
    }
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    You need to do

    place = num / x;
    num = num % x;

    at each step along the way.
    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.

  3. #3
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    I did this once for printing the words on a che(ck|que). what I did was to split the number up into 3-digit groupings. that way I only had to translate 3 digits into words, and then add a "thousand" or "million" or whatever to the end for each group.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. convert numbers to words
    By justin_lake888 in forum C++ Programming
    Replies: 3
    Last Post: 09-23-2008, 01:08 PM
  2. Replies: 5
    Last Post: 12-21-2007, 01:38 PM
  3. Numbers To words
    By Sesshokotsu in forum C Programming
    Replies: 3
    Last Post: 10-05-2006, 05:30 PM
  4. program to convert numbers into words
    By Kingsley in forum C Programming
    Replies: 5
    Last Post: 07-01-2006, 07:50 PM
  5. Numbers In Words
    By year2038bug in forum C Programming
    Replies: 2
    Last Post: 09-08-2005, 10:21 AM