Thread: convert numbers to words

  1. #1
    Registered User
    Join Date
    Sep 2008
    Posts
    1

    Question convert numbers to words

    hello... I am trying to convert 2 digit numbers to words: example 11= eleven.

    so far I have:

    Code:
    #include <iostream>
    using namespace std;
    
    cout<<"Enter a two-digit number: ";
    
    cin>>number;
    
    if(number>99)
    {
     cout<<"Number out of range";
     exit(0);
    }
    
    
    if(number==0)
    {
     cout<<"Zero";
     exit(0);
    }
    
    temp=number;
    
    digit=number%10;                    
    last_two=digit;
    number=number/10;
    digit=number%10;
    last_two=(digit*10)+last_two;
    
    while(temp!=0)                
    {
     temp=temp/10;
     digit++;
    }
    
    cout<<"You have entered the number ";
    
    
    int num;
    {
    switch(num)
    {
     case 0: 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;
    }
    return;
    }
    can someone help? thanks

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    It looks like you forgot to put your code in your main function, because there is no main function.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    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.

  4. #4
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    Err, you have a lot of unesesary(sp?) code
    1) You read a number for the input. If it is greater than 99 then you exit. Fine. You don't need ANYTHING else. You are done with the input. It is guaranteed to be a two digit number.
    2) Since you have that number use it in the switch statement to switch. You will need 100 cases though! But that is all. One variable, one input, one if(number >99) and one switch and you are done.
    3) Of course the above is too simple. So you will probably want to check the 1st digit. If it is less than 2 then have the case you have. If it is greater then have another switch. There you print "twenty", "thirty" etc etc. Then you go back the first swith and print the 2nd digit. And you are done.

    Create two functions if you want. One that returns a string from 0 to 19, the other prints the first digit (twenty, forty etc). So you would have:
    Code:
    if (greater than 20)
       cout << "number is:" << digit1(number / 10) << digit2(number %10) << endl
    if (less than 20)
       cout << "number is:" << digit2(number) endl
    That will leave one problem. If the number is greater than 20 and the 2nd digit is 0, since it will print like "thirty zero". So use one more if (number %10 != 0) in the first if and you can guess the rest

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Converting Numbers to Words
    By denizengt in forum C Programming
    Replies: 20
    Last Post: 11-05-2003, 09:19 PM
  2. C++ program help(converting numbers to english words)
    By bama1 in forum C++ Programming
    Replies: 4
    Last Post: 10-08-2002, 01:17 AM
  3. Convert Phone Letters To Numbers
    By nicolobj in forum C Programming
    Replies: 4
    Last Post: 10-03-2002, 03:53 PM
  4. A (complex) question on numbers
    By Unregistered in forum C++ Programming
    Replies: 8
    Last Post: 02-03-2002, 06:38 PM
  5. Replies: 13
    Last Post: 01-22-2002, 04:38 AM

Tags for this Thread