Thread: newbie here.. pls help me in this program!

  1. #1
    Registered User
    Join Date
    Feb 2006
    Posts
    14

    newbie here.. pls help me in this program!

    problem is i need to input an integer and i must produce an out of it in word form...

    example:
    input=1
    output=one

    input=300
    output=three hundred

    pls help me...
    thanks....

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Introducing... the search feature! Shhhh!
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Registered User
    Join Date
    Dec 2005
    Location
    Australia - Melbourne
    Posts
    63
    i wrote this program awhile ago when i was just beginning to program - i would probably do many things different (use switch instead of if ladder that's one).

    anyway i hope it is a help to you.

    Code:
    #include <stdio.h>
    #include <string.h>
    
    int gname1(char, char n0[]);
    int gname2(char, char, char n11[]);
    
    int main () {
        
        char line[20], name[20];
        unsigned int x, y = 0, digits;
        
        printf("Enter number to convert into text: ");
        fgets(line, 20, stdin);
        
        digits = strlen(line) -1;
        
        /* check if input is valid */
        for ( x=0; x < (strlen(line) -1); ++x) {
            if ((line[x] < 48) || (line[x] > 57)) ++y; 
        }    
        
        
        if (y != 0) printf("Input is invalid");
        else {
            for ( x=0; x < (strlen(line) - 1); ++x) {
                /* if digits is 2, 5, 8 send two characters to process */
                if ( line[x] != '0') { 
                    if (digits == 2 || digits == 5 || digits == 8) {
                        gname2(line[x], line[x+1], name);
                        --digits;
                        ++x;
                    }    
                    else {
                        gname1(line[x], name);
                    }
                
                    printf("%s", name);
                
                    if ((digits == 3 || digits == 6 || digits == 9) && name[0] != '\0') {
                        printf("hundred ");
                        if (line[x+1] != '0' || line[x+2] != '0') printf("and ");
                    }    
                    else if (digits == 4) printf("thousand ");
                    else if (digits == 7) printf("million ");
                }    
                
                --digits;
                
            }    
        }
        
        while ( getchar() != '\n') ;
        
        return 0;
    }
    
    int gname1(char unit, char name[]) {
        
        if (unit == '0') name[0] = '\0';
        else if (unit == '1') strcpy(name, "one ");
        else if (unit == '2') strcpy(name, "two ");
        else if (unit == '3') strcpy(name, "three ");
        else if (unit == '4') strcpy(name, "four ");  
        else if (unit == '5') strcpy(name, "five ");
        else if (unit == '6') strcpy(name, "six ");
        else if (unit == '7') strcpy(name, "seven ");
        else if (unit == '8') strcpy(name, "eight ");
        else if (unit == '9') strcpy(name, "nine ");
        
    }
    
    int gname2(char d1, char d2, char name[]) {
        
        char unit[20];
        
        if (d1 == '1' && d2 == '0') strcpy(name, "ten ");
        else if (d1 == '1' && d2 == '1') strcpy(name, "eleven ");
        else if (d1 == '1' && d2 == '2') strcpy(name, "twelve ");
        else if (d1 == '1' && d2 == '3') strcpy(name, "thirteen ");
        else if (d1 == '1' && d2 == '4') strcpy(name, "fourteen ");
        else if (d1 == '1' && d2 == '5') strcpy(name, "fifteen ");
        else if (d1 == '1' && d2 == '6') strcpy(name, "sixteen ");
        else if (d1 == '1' && d2 == '7') strcpy(name, "seventeen ");
        else if (d1 == '1' && d2 == '8') strcpy(name, "eighteen ");
        else if (d1 == '1' && d2 == '9') strcpy(name, "nineteen ");
        else if (d1 == '2') { 
            strcpy(name, "twenty ");
            gname1(d2, unit);
            strcat(name, unit);
        }
        else if (d1 == '3') { 
            strcpy(name, "thirty ");
            gname1(d2, unit);
            strcat(name, unit);
        }    
        else if (d1 == '4') {
            strcpy(name, "forty ");
            gname1(d2, unit);
            strcat(name, unit);
        }
        else if (d1 == '5') {
            strcpy(name, "fifty ");
            gname1(d2, unit);
            strcat(name, unit);
        }
        else if (d1 == '6') {
            strcpy(name, "sixty ");
            gname1(d2, unit);
            strcat(name, unit);
        }
        else if (d1 == '7') {
            strcpy(name, "seventy ");
            gname1(d2, unit);
            strcat(name, unit);
        }
        else if (d1 == '8') {
            strcpy(name, "eighty ");
            gname1(d2, unit);
            strcat(name, unit);
        }
        else if (d1 == '9') {
            strcpy(name, "ninety ");
            gname1(d2, unit);
            strcat(name, unit);
        }
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. LINK LIST program ....Pls HELP....??
    By shubhamjain_007 in forum C Programming
    Replies: 4
    Last Post: 04-19-2008, 11:48 PM
  2. Using variables in system()
    By Afro in forum C Programming
    Replies: 8
    Last Post: 07-03-2007, 12:27 PM
  3. Newbie - cubic polynomial program - help!
    By jaffa in forum C Programming
    Replies: 1
    Last Post: 03-27-2006, 05:52 AM
  4. Program doesn't always do whats expected... any ideas? (newbie)
    By photoresistor in forum C++ Programming
    Replies: 4
    Last Post: 12-07-2002, 02:49 AM
  5. Pls explain how this program works...
    By Unregistered in forum C Programming
    Replies: 9
    Last Post: 01-05-2002, 09:53 AM