Help Please.

This is a discussion on Help Please. within the C Programming forums, part of the General Programming Boards category; hello, Here's the deal, i am writing a cprogram for the first time and i have to convert numbers into ...

  1. #1
    Registered User
    Join Date
    Sep 2001
    Posts
    9

    Question Help Please.

    hello,

    Here's the deal, i am writing a cprogram for the first time and i have to convert numbers into words. Sucah as a check has the amount written in numbers and then the words. i have been looking for the proper coding for about 1.5 weeks and i am still stuck. any suggestions will be helpful I was told this was a simple function to do but for the life of me i can't figure it out so if someone could point me in the right direction i woulkd appreciate it..

    Thanks
    Frustraded student...=(

  2. #2
    Mayor of Awesometown Govtcheez's Avatar
    Join Date
    Aug 2001
    Location
    MI
    Posts
    8,825
    Look up the function itoa.

  3. #3
    Lead Moderator kermi3's Avatar
    Join Date
    Aug 1998
    Posts
    2,595
    Are you looking to change whole numbers in to their actual meaning or to change into Ascii value?

    ie:

    Two into 2

    or

    Two into 84+119+111

    (84, 119, and 111 are the Ascii value of 'T', 'w', and 'o' respectively)
    Kermi3

    If you're new to the boards, welcome and reading this will help you get started.
    Information on code tags may be found here

    - Sandlot is the highest form of sport.

  4. #4
    Registered User
    Join Date
    Sep 2001
    Posts
    9
    2 into Two is what i am looking to do..

    as for looking up the itoa function i have no listing of that in my book.. is it a function in C ??

    Thanks

  5. #5
    Mayor of Awesometown Govtcheez's Avatar
    Join Date
    Aug 2001
    Location
    MI
    Posts
    8,825
    Oh sorry, I misunderstood...

    itoa is a library function that would change the integer value 3145 into the string "3145". It won't really help you...

  6. #6
    Registered User alex's Avatar
    Join Date
    Sep 2001
    Posts
    132
    Hi Krullt!

    So how far DID you get... Could you post some code? Maybe you can start with something like this.

    Code:
    #include <stdio.h>
    char dec1[10]={"Zero", "One", ...}; 
    main()
    {
       int val;
       scanf("%i", &val);
    
       if(val>=0 && val<10)
          printf("%i %s\n", val, dec1[val]);
       else
          printf("Can't count to ten yet...\n");
    }
    Hope this helps...
    alex

  7. #7
    Registered User
    Join Date
    Sep 2001
    Posts
    9
    ok here is what i have written.. the problem i am having is i don't know how to take my calculation and convert the answer in to words.

    IE

    $320 to three hundred and twenty


    #include <stdio.h>

    int main(void)
    {
    /*THIS PROGRAM IS DESIGNED TO PRINT OUT A CHECK*/

    printf("****************************************** **********\n");
    printf("* Acme Computer Company *\n");
    printf("* 2345 First Street *\n");
    printf("* Los Angeles, CA 10101-0001 *\n");
    printf("* *\n");
    printf("* *\n");
    printf("* *\n");
    printf("* William H. Taylor ");
    Money (); /*CALCULATION OF AMOUNT TO BE PAYED*/
    printf("* *\n");
    printf("* *\n");
    printf("* *\n");
    printf("* John. Q Smith *\n");
    printf("* *\n");
    printf("* :987654321|: 1236547896542||*58749 *\n");
    /*ACCOUNT NUMBER*/
    printf("****************************************** **********\n");
    return 0;
    }

    void Money ()
    {
    /*BASIC CALCULATION TO COME UP WITH THE AMOUNT TO BE PAYED*/
    float fica, insurance, gross, net;

    gross = 500;
    fica = 500 * .075;
    insurance = 500 * .01;
    net = gross - (fica + insurance);
    printf(" $%.2f *\n", net);
    }

  8. #8
    Registered User alex's Avatar
    Join Date
    Sep 2001
    Posts
    132
    Originally posted by Krullt
    ok here is what i have written.. the problem i am having is i don't know how to take my calculation and convert the answer in to words.

    IE

    $320 to three hundred and twenty

    printf(" $%.2f *\n", net);
    You could use code like this:

    Code:
    int cents, dollar100, dollar10, dollar...
    ...
    cents=100*net; /* compute the number of dollarcents */
    dollar100=cents/10000; /* compute the "3" in your example */
    cents-=dollar100*10000;
    dollar10=cents/1000; /* the "2" */
    cents-=dollar10*1000;
    dollars=cents/100; /* the "0" */
    cents-=dollars*100;
    cent10=cents/10; /* first decimal after . */
    cents-=cent10*10; /* and finally the second decimal after . */
    An easier way (probably what a more experienced C programmer would use) is by using the function sprintf. It does the same job as printf, but puts the formatted output into a string and not onto the screen.

    Code:
    char netstr[9];
    ...
    sprintf(valstr, "%08.2f", net);
    /* printf("%s\n", netstr); */
    /* printf("%i%i%i dollars", netstr[2]-'0', netstr[3]-'0', netstr[4]-'0');
    printf(" and %i%i cents\n", netstr[6]-'0' netstr[7]-'0');
    I think this gives all the pieces of math you need to convert the value into english words...

    Hope this helps...
    alex

  9. #9
    Registered User
    Join Date
    Aug 2001
    Posts
    101
    Here's one way to do it. Not perfect, mind.
    Code:
    #include <stdio.h>
    #include <string.h>
    #include <math.h>
    
    char* GetHundreds(char* buffer, const char* num)
    {
        char*   pbuffer = buffer;
        char*   Numbers[] =
        {
            "one", "two", "three", "four", "five", "six", "seven",
            "eight", "nine", "ten", "eleven", "twelve", "thirteen",
            "fourteen", "fifteen", "sixteen", "seventeen", "eightteen",
            "nineteen", "twenty", "thirty", "fourty", "fifty", "sixty",
            "seventy", "eighty", "ninety"
        };
    
        if(num[0] != '0')
        {
            // xyz, x != 0 (ie: 100)
            sprintf(pbuffer, "%s hundred ", Numbers[num[0] - '1']);
            pbuffer += strlen(pbuffer);
        }
    
        if(num[1] != '0' || num[2] != '0')
        {
            if(num[0] != '0')
            {
                strcpy(pbuffer, "and ");
                pbuffer += strlen(pbuffer);
            }
            if(num[1] == '0')
                // x0z, z != 0 (ie: 101)
                sprintf(pbuffer, "%s ", Numbers[num[2] - '1']);
            else if(num[1] == '1')
                // x1z (ie: 111)
                sprintf(pbuffer, "%s ", Numbers[num[2] - '1' + 10]);
            else if(num[2] != '0')
                // xyz, y != 0, y != 1, z != 0 (ie: 121)
                sprintf(pbuffer, "%s %s ", Numbers[num[1] - '2' + 19], Numbers[num[2] - '1']);
            else
                // xy0, y != 0, y != 1 (ie: 120)
                sprintf(pbuffer, "%s ", Numbers[num[1] - '2' + 19]);
        }
    
        return buffer;
    }
    
    // Writes the word equivalent of Number in buffer.
    // Returns buffer.
    char* IntToWords(char* buffer, int Number)
    {
        char    num[13];
        char*   pnum = num;
        char*   pbuffer = buffer;
        char*   Numbers[] = { "milliard ", "million ", "thousand " };
        int     i;
        
        if(Number == 0)
        {
            strcpy(buffer, "zero");
            return buffer;
        }
    
        // Check sign
     if(Number < 0)
        {
            strcpy(pbuffer, "minus ");
            pbuffer += 6;
        }
    
        sprintf(num, "%012d", abs(Number));
    
        // milliards, millions and thousands
     for(i = 0; i < 3; i++, pnum += 3)
            if(pnum[0] != '0' || pnum[1] != '0' || pnum[2] != '0')
            {
                GetHundreds(pbuffer, pnum);
                strcat(pbuffer, Numbers[i]);
                pbuffer += strlen(pbuffer);
            }
        // hundreds
     if(pnum[0] != '0' || pnum[1] != '0' || pnum[2] != '0')
            GetHundreds(pbuffer, pnum);
    
        return buffer;
    }
    
    int main()
    {
        char    buffer[200];
        int     x = 1234567890;
    
        printf("%d - %s\n", x, IntToWords(buffer, x));
    
        return 0;
    }

    The above code produces the output:
    1234567890 - one milliard two hundred and thirty four million five hundred and sixty seven thousand eight hundred and ninety
    - lmov

Popular pages Recent additions subscribe to a feed

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