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.
Not finished yet obviously, but I hit a problem on the line with: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]]); } }
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...?



LinkBack URL
About LinkBacks
, though i'm not a CS major, so it really rustles my jimmies.


