Like Tree2Likes
  • 1 Post By oogabooga
  • 1 Post By oogabooga

converting integers to words

This is a discussion on converting integers to words within the C Programming forums, part of the General Programming Boards category; this code is giving me stress.... Code: //FileName: int_converter.c //Author: Nyah Check, Chairman && CEO at INK Corp. //Usage: This ...

  1. #1
    Registered User
    Join Date
    Nov 2011
    Location
    Douala, Cameroon
    Posts
    187

    converting integers to words

    this code is giving me stress....


    Code:
    //FileName: int_converter.c
    //Author: Nyah Check, Chairman && CEO at INK Corp.
    //Usage: This program converts numbers between 0 and a billion to words.
    //INK Corp. 2012.
    
    #include <stdio.h>
    #include <stdlib.h>
    #include <ctype.h>
    
    #define MAX 10
    
    int read_int(int *, int );
    int analyse_print(int *, int);
    
    int num[MAX];
    int i;
    
    //Main: This  function calls two main functions; read_int, analyse_int; respectively.
    int main(void)
    {
        printf("\nEnter a number between 1 and a billion for it to be converted to words.");
        printf("\nEnter 'Q' to QUIT.");
    
        for( ; ; )
        {
            read_int(num, MAX);
            analyse_print(num, i);
        }
        return 0;
    }
    
    //read_int: This function reads the integers from input checks and stores them in an array.
    int read_int(int num[], int n)
    {
        int ch;
    
        i = 0;
        printf("\nEnter number Here: ");
    
        while( (ch = getchar())!= '\n')
        {
    
            if ( ch == 'Q' || ch == 'q')
            {
                printf("\nExiting....");
                printf("\nThanks for using our software; we would like to receive your comments and suggestions to better our services.");
                printf("\nINK Corp. 2012.");
                exit(EXIT_SUCCESS);
            }
    
            else if ( i < n)
            {
                num[i++] = ch;
            }
        }
        printf("\n%d\n", i);
    
        return i;
    }
    
    //analyse_int: analyses the number to determine its range, hundred to million to billion. and prints the number in words.
    int analyse_print(int num[], int m)
    {
        //These array contain the keywords that would be needed for the conversion.
      char *sval[] = { " ", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine"};
      char *tval[] = {" " , "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen"};
      char *hval[] = {" " ," ","Twenty"," Thirty", "Forty", "Fifty","Sixty", "Seventy", "Eighty", "Ninety"};
      char *nval[] = {"Hundred", "Thousand", "Million", "Billion"};
    
        switch(m)
        {
            //This converts numbers between 1 to 10.
            case 1:                        if (num[0] == 0)
                                            {
                                                printf("\nZero.");
                                            }
    
                                          else
                                                printf("\n%s", sval[num[0]]);
    
                                          break;
    
            //This converts numbers between 10 and 99.
            case 2:                          if ( num[0] == 1)
                                             {
                                                printf("\n%s", tval[num[1]]);
                                             }
                                            else
                                            {
                                                printf("\n%s %s", hval[num[0]],sval[num[1]]);
                                            }
    
                                            break;
    
            //This converts numbers between 100 and 999.
            case 3:
                                            if ( num[1] == 1)
                                            {
                                                printf("\n%s hundred   and %s", sval[num[0]], tval[num[2]]);
                                            }
    
                                            else
                                                printf("\n%s hundred  and %s %s", sval[num[0]], hval[num[1]], sval[num[2]]);
                                            break;
    
            //This converts between 1000 and 9999.
            case 4:                          if ( num[2] == 1)
                                            {
                                                printf("\n%s Thousand %s hundred and %s ", sval[num[0]], sval[num[1]], tval[num[3]]);
                                            }
    
                                            else
                                                printf("\n%s Thousand %s Hundred and %s %s ", sval[num[0]], sval[num[1]], hval[num[2]], num[3]);
    
                                            break;
    
            //10,000 and 99,999.....
            case 5:
                                            if ( num[0] == 1 && num[3] == 1)
                                            {
                                                printf("\n%s thousand %s hundred and %s.",tval[num[1]], sval[num[2]],tval[num[5]]);
                                            }
    
                                            else if ( num[0] == 1)
                                            {
                                                printf("\n%s Thousand %s Hundred and %s %s", tval[num[1]], sval[num[2]],nval[0], hval[num[3]], sval[num[4]]);
                                            }
    
                                            else if ( num[3] == 1)
                                            {
                                                printf("\n%s %s Thousand, %s Hundred and  %s ", hval[num[0]], sval[num[1]], sval[num[2]],tval[num[4]]);
                                            }
                                            else
                                            {
                                                printf("\n%s %s Thousand, %s Hundred and %s %s ", hval[num[0]], sval[num[1]], sval[num[2]],hval[num[3]], sval[num[4]]);
                                            }
    
                                            break;
    
            //100,000 and 999,999...
            case 6:                         if ( num[1] == 1 && num[4] == 1)
                                            {
                                                printf("\n%s Hundred and %s Thousand, %s Hundred and %s ",sval[num[0]], tval[num[2]],sval[num[3]], tval[num[5]]);
                                            }
    
                                            else if ( num[1] == 1)
                                            {
                                                int j = num[2];
                                                printf("\n%s Hundred %s thousand  %s hundred and %s %s",sval[num[0]], tval[j], sval[num[3]], hval[num[4]], sval[num[5]]);
                                            }
    
                                            else if ( num[4] == 1)
                                            {
                                                printf("\n%s hundred %s %s thousand, %s hundred and %s ",sval[num[0]], hval[num[1]], sval[num[2]], sval[num[3]],tval[num[5]]);
                                            }
    
                                            else
                                            {
                                                printf("\n%s hundred and %s %s thousand, %s hundred and %s %s ",sval[num[0]], hval[num[1]], sval[num[2]], sval[num[3]],hval[num[4]], sval[num[5]]);
                                            }
    
                                            break;
    
            //1,000,000 and 9,999,999.
            case 7:                         if ( num[2] == 1 && num[5] == 1 )
                                            {
                                                printf("\n%s million, %s hundred and %s thousand, %s hundred and %s ", sval[num[0]], sval[num[1]], tval[num[3]], sval[num[4]], tval[num[6]]);
                                            }
    
                                            else if ( num[2] == 1)
                                            {
                                                printf("\n%s million,%s hundred and %s thousand, %s hundred and %s %s",sval[num[0]],sval[num[1]], tval[num[3]], sval[num[4]], hval[num[5]], sval[num[6]]);
                                            }
    
                                            else if ( num[5] == 1)
                                            {
                                                printf("\n%s %s,%s %s and %s %s %s, %s %s and %s ",sval[num[0]],nval[2],sval[num[1]], nval[0], hval[num[2]], sval[num[3]],nval[1], sval[num[4]],nval[0],tval[num[6]]);
                                            }
    
                                            else
                                            {
                                                printf("\n%s %s,%s %s and %s %s %s, %s %s and %s %s ",sval[num[0]], nval[2], sval[num[1]], nval[0], hval[num[2]], sval[num[3]],nval[1], sval[num[4]],nval[0],hval[num[5]], sval[num[6]]);
                                            }
    
                                            break;
    
            //10,000,000 and 99,999,999....
            case 8:                         if ( num[0] == 1 && num[3] == 1 && num[6] == 1)
                                            {
                                                printf("\n%s %s, %s %s and %s %s, %s %s and %s", tval[num[1]], nval[2], sval[num[2]], nval[0], tval[num[4]], nval[1], sval[num[5]], nval[0], tval[num[7]]);
                                            }
    
                                            else if ( num[0] == 1 && num[3])
                                            {
                                                printf("\n%s %s, %s %s and %s %s, %s %s and %s %s", tval[num[1]], nval[2], sval[num[2]], nval[0], tval[num[4]], nval[1], sval[num[5]], nval[0], hval[num[6]], sval[num[7]]);
                                            }
    
                                            else if ( num[0] == 1 && num[6] == 1)
                                            {
                                                printf("\n%s %s, %s %s and %s %s %s , %s %s and %s", tval[num[1]], nval[2], sval[num[2]], nval[0], hval[num[3]], sval[num[4]],nval[1], sval[num[5]],nval[0], tval[num[7]]);
                                            }
    
                                            else if ( num[0] == 1)
                                            {
                                                printf("\n%s %s, %s %s and %s %s %s, %s %s and %s %s ", tval[num[1]], nval[2], sval[num[2]], nval[0], hval[num[3]], sval[num[4]],nval[1], sval[num[5]],nval[0], hval[num[6]], sval[num[7]]);
                                            }
    
                                            else if ( num[3] == 1 && num[6] == 1)
                                            {
                                                printf("\n%s %s %s, %s %s and %s %s, %s %s and %s", hval[num[0]], sval[num[1]], nval[2], hval[num[2]], nval[0], tval[num[4]], nval[1],sval[num[5]], nval[0], tval[num[7]]);
                                            }
    
                                            else if ( num[3] == 1)
                                            {
                                                printf("\n%s %s %s, %s %s and %s %s, %s %s and %s %s", hval[num[0]], sval[num[1]], nval[2], hval[num[2]], nval[0], tval[num[4]],nval[1],sval[num[5]], nval[0], hval[num[6]], sval[num[7]]);
                                            }
    
                                            else if ( num[6] == 1)
                                            {
                                                printf("\n%s %s %s, %s %s and %s %s %s, %s %s and %s ",  hval[num[0]], sval[num[1]], nval[2], hval[num[2]], nval[0], hval[num[3]], sval[num[4]], nval[1],sval[num[5]], nval[0], tval[num[7]]);
                                            }
    
                                            else
                                                printf("\n%s %s %s, %s %s and %s %s %s, %s %s and %s %s ", hval[num[0]], sval[num[1]], nval[2], hval[num[2]], nval[0], hval[num[3]], sval[num[4]], nval[1],sval[num[5]], nval[0], hval[num[6]],sval[num[7]]);
    
                                            break;
    
    
            //100,000,000 and 999,999,999......
            case 9:                             if ( num[1] == 1 && num[4] == 1 && num[7] == 1)
                                                {
                                                    printf("\n %s %s  and %s %s, %s %s and %s %s, %s %s and %s ", sval[num[0]], nval[0], tval[num[2]], nval[2], sval[num[3]],nval[0],tval[num[5]], nval[1], sval[num[6]], nval[0], tval[num[8]]);
                                                }
    
                                                else if( num[1] == 1 && num[4] == 1)
                                                {
                                                    printf("\n%s %s and %s %s, %s %s and %s %s, %s %s and %s %s ", sval[num[0]], nval[0], tval[num[2]], nval[2], sval[num[3]], nval[0], tval[num[5]], nval[1], sval[num[6]], nval[0], hval[num[7]], sval[num[8]]);
                                                }
    
                                                else if ( num[1] == 1 && num[7] == 1)
                                                {
                                                printf("\n%s %s and %s %s, %s, %s and %s %s %s, %s %s and %s", sval[num[0]], nval[0], tval[num[2]], nval[2], sval[num[3]],nval[0],hval[num[4]], sval[num[5]], nval[1], sval[num[6]], nval[0], tval[num[8]]);
                                                }
    
                                                else if ( num [1] == 1)
                                                {
                                                    printf("\n%s %s and %s %s, %s %s and %s %s %s, %s %s and %s %s", sval[num[0]], nval[0], tval[num[2]], nval[2], sval[num[3]],nval[0], hval[num[4]], sval[num[5]], nval[1], sval[num[6]], nval[0], hval[num[7]], sval[num[8]]);
                                                }
    
                                                else if ( num[4] == 1 && num[7] == 1)
                                                {
                                                    printf("\n %s %s and %s %s %s, %s %s and %s %s, %s %s and %s ", sval[num[0]], nval[0], hval[num[1]], sval[num[2]], nval[2], sval[num[3]],nval[0], tval[num[5]], nval[1],sval[num[6]],nval[0], tval[num[8]]);
                                                }
    
                                                else if ( num[4] == 1)
                                                {
                                                    printf("\n%s %s and %s %s %s , %s %s and %s %s, %s %s and %s %s ", sval[num[0]], nval[0], hval[num[1]], sval[num[2]], nval[2], sval[num[3]], nval[0], tval[num[5]],nval[1], sval[6], nval[0], hval[num[7]], sval[num[8]]);
                                                }
    
                                                else if ( num [7] == 1)
                                                {
                                                    printf("\n%s %s and %s %s %s, %s %s and %s %s %s, %s %s and %s ", sval[num[0]], nval[0], hval[num[1]], sval[num[2]],nval[2], sval[num[3]],nval[0], hval[num[4]], sval[num[5]], nval[1], sval[num[6]], nval[0], tval[num[8]]);
                                                }
    
                                                else
                                                    printf("\n%s %s and %s %s %s, %s %s and %s %s %s, %s %s and %s %s",sval[num[0]], nval[0], hval[num[1]], sval[num[2]],nval[2], sval[num[3]],nval[0], hval[num[4]], sval[num[5]], nval[1], sval[num[6]], nval[0], hval[num[7]], sval[num[8]]);
    
                                                break;
            default:                     fprintf(stderr, "No value entered. Exiting now...");
                                            exit (EXIT_FAILURE);
        }
    
        return 0;
    }

  2. #2
    Registered User
    Join Date
    Nov 2011
    Location
    Douala, Cameroon
    Posts
    187
    these thing has been a pain in the ass for about 2 weeks now......

  3. #3
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Are you just venting, or do you have a question?

    Just looking at your code give me a pain, not in my ... well, but still.
    claudiu likes this.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  4. #4
    Registered User javaeyes's Avatar
    Join Date
    Feb 2012
    Posts
    153
    This code makes baby Jesus cry.

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,484
    Rather than an array of digits, why not just pass a whole number, and discover the magic of /1000 and %1000
    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
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    To do this more programmatically, note that there is a double loop in the problem. The outer loop decomposes the input number by 1000's, while the (unrolled) inner loop decomposes the 100's, 10's, and 1's for each of the power's of 1000. E.g.,

    123123123
    One Hundred Twenty Three Million One Hundred Twenty Three Thousand One Hundred Twenty Three

    The "One Hundred Twenty Three" is the same for billions, millions, thousands, or ones.

    So you might try something as indicated in this (untested) pseudocode:
    Code:
    // int n is value to print
    thousands = 1000000000   // billion
    do {
        m = n / thousands;
        if m > 0:
            print hundreds, tens, ones of m
            print word based on value of thousands.
        n %= thousands;
        thousands /= 1000;
    }while thousands > 1
    iMalc likes this.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  7. #7
    Registered User
    Join Date
    Nov 2011
    Location
    Douala, Cameroon
    Posts
    187
    hey guys i have modified the code and it works perfectly now... any way thanks @oogabooga for the tip and others....

  8. #8
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    The pseudocode I posted above should have a loop condition of thousands > 0 (it should still execute once more for thousands == 1).
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  9. #9
    Registered User
    Join Date
    Nov 2011
    Location
    Douala, Cameroon
    Posts
    187
    ok but i just debugged the read_int() function and it converted successfully any way i'll still try your s its easier.... and a little more complex...thanks @oogobooga....

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Converting Integers to Strings
    By momo97 in forum C++ Programming
    Replies: 5
    Last Post: 03-04-2009, 12:36 AM
  2. converting string to integers
    By casper7 in forum C Programming
    Replies: 16
    Last Post: 06-21-2008, 10:35 AM
  3. Converting String to integers
    By Mister C in forum C Programming
    Replies: 21
    Last Post: 06-01-2005, 03:13 PM
  4. Why is this program converting to integers
    By 3DPhreak in forum C++ Programming
    Replies: 3
    Last Post: 05-31-2004, 09:44 AM
  5. Converting Integers to Binary
    By cprog in forum C Programming
    Replies: 19
    Last Post: 10-03-2002, 08:20 PM

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