Using seperate elements in a string array?

This is a discussion on Using seperate elements in a string array? within the C Programming forums, part of the General Programming Boards category; Sorry in advance for the wordiness This is for a homework assignment, so i'm not asking for help on the ...

  1. #1
    Registered User
    Join Date
    Apr 2012
    Posts
    7

    Using seperate elements in a string array?

    Sorry in advance for the wordiness

    This is for a homework assignment, so i'm not asking for help on the entire thing , though i'm not a CS major, so it really rustles my jimmies.
    The code is supposed to convert a number into words (ie 23 into twenty three)

    so far, the user inputs an int, i use sprintf to write it to a char array, then send it to a function that is supposed to convert it.

    Code:
    #include <stdio.h>
    #include <string.h>
    
    int getNumber();
    char convertToString(char *buffer);
    
    int main()
    {
        char *buffer[100];
        int num;
    
        num = getNumber();
        memset(buffer, '\0', 100);
        sprintf(buffer,"%d",num);
    
        printf("You typed: %s \n",buffer); //test
    
        convertToString(buffer);
    
    
        return 0;
    
    }
    
    int getNumber(){
    
        int num;
    
        do{
           printf("Enter an integer less than 1000: ");
           scanf("%d",&num);
        }while(num<1 || num>999);
    
        return(num);
    }
    
    char convertToString(char *buffer)
    {
        char *ones[]={"one ","two ","three ","four ","five","six ","seven ","eight ","nine "};
        char *tens[]={"ten ","eleven ","twelve ","thirteen ","fourteen ","fifteen ","sixteen ","seventeen ","eighteen ","nineteen "};
        char *twenties[]={"","twenty ","thirty ","forty ","fifty ","sixty ","seventy ","eighty ","ninety "};
        char *hundreds[]={"","","one hundred ","two hundred ","three hundred ","four hundred ","five hundred ","six hundred ","seven hundred ","eight hundred ","nine hundred "};
    
        char *temp[100];
        int length = strlen(buffer);
    
        if(length = 1)
        {
            strcpy(temp,ones[buffer[0]]);
        }
    }
    Not finished yet obviously, but I hit a problem on the line with:
    strcpy(temp,ones[buffer[0]]);
    which is supposed to take what's in buffer[0] (the first digit of the inputted number), and tell it to copy the same index of ones (so if buffer[0] = 3, ones[buffer[0]] = three, then copy it into my temp array, which integers of length 2 or 3 would be catenated to the end.

    BUT, if the user inputted number was say 34, buffer[0]=3 as i would expect, but buffer[1] is garbage. How can I fix this so the separate digits of the inputted number are in separate elements of the buffer array? Or is there an easier way to do this...?

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Before you convert the number to a string, I'd "range" the number, so if you had a number like twenty two, you wouldn't have to parse out whether it was a twelve or not, later on. (since they both begin with "tw").

    Look for an _itoa() function or you can code your own easy enough. It's not standard C, but mine is located in stdlib.h, which your program would need to include.

  3. #3
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    4,167
    BUT, if the user inputted number was say 34, buffer[0]=3 as i would expect, but buffer[1] is garbage.
    Wrong on both accounts.

    The string "34" is composed of three character values '3', '4', and '\0'. The value '3' may or may not equal the numeric value 3.

    You are essentially parsing a string for a value and then immediately serializing that value back to a string.

    Soma

  4. #4
    Registered User
    Join Date
    Apr 2012
    Posts
    7
    i got it working, thanks for the input

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 12
    Last Post: 04-20-2009, 06:44 AM
  2. Array (seperate inputs by comma)
    By chuckwag0n in forum C++ Programming
    Replies: 7
    Last Post: 04-03-2009, 01:10 PM
  3. Array (seperate inputs by comma)
    By chuckwag0n in forum C Programming
    Replies: 1
    Last Post: 04-02-2009, 01:01 PM
  4. Converting to expression to string of array elements
    By Sailors in forum C Programming
    Replies: 12
    Last Post: 07-26-2007, 03:01 PM
  5. Array having Integer and String elements
    By skm in forum C++ Programming
    Replies: 7
    Last Post: 12-16-2004, 02:44 AM

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