Creating a string for decimals

This is a discussion on Creating a string for decimals within the C Programming forums, part of the General Programming Boards category; To Anyone Who Can Help- I'm in the midst of typing a code for a program where the user enters ...

  1. #1
    Registered User
    Join Date
    Sep 2012
    Posts
    32

    Question Creating a string for decimals

    To Anyone Who Can Help-

    I'm in the midst of typing a code for a program where the user enters a numerical dollar amount as a string and has it convert to word form.
    Ex) $67,218.21 becomes Sixty-seven thousand, two hundred eighteen and twenty one hundredth dollars.

    For debugging purposes, I've had it print out the string length count with all characters, which it shows. It also gives out an error for things such as sign and magnitude.

    The main thing that has been hampering me in my process is the decimal place. I'm trying to get the program to, upon seeing the decimal, take the string values past the right decimal and put them into a separate string, and put NULL values where the old values were. My problem is that it isn't modifying these values; the user entry string remains unchanged, and the decimal string has garbage.

    I especially know it doesn't work because I'm trying to have it subtract from the previous length count and add to a decimal length count, and then print out both strings, neither of which the program does. What exactly is it that I'm doing wrong?

    Code:
    #include <stdio.h>
    #include <conio.h>
    #include <string.h>
    #include <ctype.h>
    
    typedef char* string;
    
    int dec_con(char decimal[]);
    
    int main(){
    
        char amount[21];                        //variable for data entry
        char decimal[2];                        //variable for modified decimal places            
        int length = 0;                            //length counter
        int length_2 = 0;                        //decimal length counter
        int i, j;                                //variables for for loop
    
        puts("Please enter the amount you wish to convert into words.");
        scanf("%20[^\n]", amount);
    
        for (i = 0; i < amount[i]; i++)            //count string length
            length++;
    
        printf("%i\n", length);                    //DEBUG
    
        if ((unsigned)strlen(amount) > 19){        //error for length, then program break
            printf("Error. Magnitude must be than one trillion.\n");
            return 0;
        }
    
        if (amount[0] == '-'){                    //error for sign, then program break
            printf("Error. Magnitude must be positive.\n");
            return 0;
        }
    
        for (i = 0; i < amount[i]; i++){        //grouping by columns
            if (amount[i] == ','){
            }
        }
    
        for (i = 0; i < amount[i]; i++){        //check for decimals
            if (amount[i] == '.'){
                for (j = 0; j < decimal[j]; j++){ //
                    decimal[j] = amount[j+1];    //put variables in the new string
                    amount[i+j] = NULL;            //replace its original location with NULL
                    length--;                    //subtract from original count
                    length_2++;                    //add to decimal count
                }
            }
        }
    
        printf("%i %i\n", length, length_2);    //DEBUG
    
        for (i = 0; i < amount[i]; i++)
            printf("%c", amount[i]);
        puts("");
    
        for (i = 0; i < decimal[i]; i++)
            printf("%c", decimal[i]);
        puts("");
    
    
        dec_con(decimal);                        //calls the decimal conversion function
    }
    
    int dec_con(char decimal[]){
    
            if (decimal[0] == '0'){
                if (decimal[1] == '0')
                    printf("dollars.");
                if (decimal[1] == '1')
                    printf("and one hundredth dollars.");
                if (decimal[1] == '2')
                    printf("and two hundredths dollars.");
                if (decimal[1] == '3')
                    printf("and three hundredths dollars.");
                if (decimal[1] == '4')
                    printf("and four hundredths dollars.");
                if (decimal[1] == '5')
                    printf("and five hundredths dollars.");
                if (decimal[1] == '6')
                    printf("and six hundredths dollars.");
                if (decimal[1] == '7')
                    printf("and seven hundredths dollars.");
                if (decimal[1] == '8')
                    printf("and eight hundredths dollars.");
                if (decimal[1] == '9')
                    printf("and nine hundredths dollars.");
                    }
            else if (decimal[0] == '1'){
                if (decimal[1] == '0')
                    printf("and one tenth dollars.");
                    if (decimal[1] == '1')
                    printf("and eleven hundredths dollars.");
                    if (decimal[1] == '2')
                    printf("and twelve hundredths dollars.");
                    if (decimal[1] == '3')
                    printf("and thirteen hundredths dollars.");
                    if (decimal[1] == '4')
                    printf("and fourteen hundredths dollars.");
                    if (decimal[1] == '5')
                    printf("and fifteen hundredths dollars.");
                    if (decimal[1] == '6')
                    printf("and sixteen hundredths dollars.");
                    if (decimal[1] == '7')
                    printf("and seventeen hundredths dollars.");
                    if (decimal[1] == '8')
                    printf("and eighteen hundredths dollars.");
                    if (decimal[1] == '9')
                    printf("and nineteen hundredths dollars.");
                    }
            else{
                if (decimal[0] == '2')
                    printf("and twenty ");
                if (decimal[0] == '3')
                    printf("and thirty ");
                if (decimal[0] == '4')
                    printf("and forty ");
                if (decimal[0] == '5')
                    printf("and fifty ");
                if (decimal[0] == '6')
                    printf("and sixty ");
                if (decimal[0] == '7')
                    printf("and seventy ");
                if (decimal[0] == '8')
                    printf("and eightty ");
                if (decimal[0] == '9')
                    printf("and ninety ");
    
    
                if (decimal[1] == '1')
                    printf("one hundredth dollars.");
                if (decimal[1] == '2')
                    printf("two hundredths dollars.");
                if (decimal[1] == '3')
                    printf("three hundredths dollars.");
                if (decimal[1] == '4')
                    printf("four hundredths dollars.");
                if (decimal[1] == '5')
                    printf("five hundredths dollars.");
                if (decimal[1] == '6')
                    printf("six hundredths dollars.");
                if (decimal[1] == '7')
                    printf("seven hundredths dollars.");
                if (decimal[1] == '8')
                    printf("eight hundredths dollars.");
                if (decimal[1] == '9')
                    printf("nine hundredths dollars.");
    
                
                    }
            return 0;
    
    
    
    }
    Last edited by William Putnam; 09-25-2012 at 03:42 PM. Reason: finished a sentence.

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    You can easily find and truncate the digits on the left hand side of the decimal point, using a while(s[i] != '.' && i++ < len), where "len" is an int variable, with the length of the string:
    int len = strlen(s);

    For all the rest of it, William, William, William, -- tsk tsk! Our numbers are based on 10 - and every power of 10 gives us a new word: one's, ten's, hundreds, thousands, etc.

    If your number is a string, then after removing the decimal point and any digits to the right hand side of that decimal point, then you can simply use strlen(charArrayName), to tell you what word is the proper word to start with:
    Code:
    words[5][20]= {
    {""},            //no one
    {""},            //no ten
    {"hundred"},
    {"thousand"},  //no ten or hundred thousand
    {"million"}
    };
    Note that if you remove the end of string char '\0', along with the decimal point, that you will want to replace it as the right most char in any string. Otherwise, your string is just a gaggle of chars, not a string in C.

    So, your code is WAY Rube Goldberg, and using our number base of 10, you can shorten it, using an array of words. We don't use "5 ten thousands" though. So set it up to display "50 thousand", as hinted in the code above.

    That will seriously shorten your code, and the time it takes to write it. Thinking cap required.
    Last edited by Adak; 09-25-2012 at 04:09 PM.

  3. #3
    qny
    qny is offline
    Registered User
    Join Date
    Sep 2012
    Posts
    355
    Code:
        for (i = 0; i < amount[i]; i++)            //count string length
            length++;
    No!

    TIP
    Instead do (I see you have already included <string.h>, which is where the prototype for strlen() is)
    Code:
    length = strlen(amount);

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Welcome to the forum, William!

    Answering your question

    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main(void) {
       int i, len;
       char snum[15]={"1234567890.123"};
       char newnum[15];
       i=0;
       len=strlen(snum);
       while(snum[i] != '.' && i < len) {
          newnum[i]=snum[i]++;
       }
       newnum[i]='\0';
       printf("%s\n",newnum);
     
       printf("\n");
       return 0;
    }
    Now everything > len in snum, can go into another string, if you want.
    Last edited by Adak; 09-25-2012 at 04:50 PM.

  5. #5
    Registered User
    Join Date
    Sep 2012
    Posts
    32
    Quote Originally Posted by Adak View Post
    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main(void) {
       int i, len;
       char snum[15]={"1234567890.123"};
       char newnum[15];
       i=0;
       len=strlen(snum);
       while(snum[i] != '.' && i < len) {
          newnum[i]=snum[i]++;
       }
       newnum[i]='\0';
       printf("%s\n",newnum);
     
       printf("\n");
       return 0;
    }
    adak-

    Thanks for your advice. I'm starting to understand how to take care of the word sorting. However, when I tried to run this sample code in my sandbox to see how it processed, there was literally no output; just two empty rows. There were no errors or warnings on my compiler.

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Sorry about that - I took the working program I had, and "improved it", to make it a bit more clear.

    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main(void) {
       int i, len;
       char snum[15]={"1234567890.123"};
       char newnum[15];
       i=0;
       len=strlen(snum);
       while(snum[i] != '.' && i < len) {
          newnum[i]=snum[i];
          ++i;
       }
       newnum[i]='\0';
       printf("%s\n",newnum);
     
       printf("\n");
       return 0;
    }

  7. #7
    Registered User
    Join Date
    Sep 2012
    Posts
    32
    Quote Originally Posted by Adak View Post
    Sorry about that - I took the working program I had, and "improved it", to make it a bit more clear.
    Thanks for the update, Adak. It works properly now.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. why its not creating a string??
    By transgalactic2 in forum C Programming
    Replies: 4
    Last Post: 04-11-2009, 01:36 PM
  2. Creating very long decimals
    By scwizzo in forum C++ Programming
    Replies: 9
    Last Post: 08-25-2008, 01:57 AM
  3. File creating with string for name
    By Loknar Gor in forum C++ Programming
    Replies: 13
    Last Post: 09-23-2006, 08:38 AM
  4. I need help creating a string class
    By incognito in forum C++ Programming
    Replies: 1
    Last Post: 01-19-2002, 04:48 PM
  5. creating a string in a buffer
    By carrythe0 in forum C Programming
    Replies: 1
    Last Post: 10-01-2001, 11:41 PM

Tags for this Thread


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