I'm new to C and I was doing okay in my class(online) until the last couple of programs. They're getting more complicated and I'm not much of a strategist. But, I'm trying and I'm frustrated.
I've been working on this one for a while (it's past due) and my ulimate goal is to convert a number into words for a paycheck.
What I'm trying to do here is convert the double value to an int and put the int into a char array, so that eventually the "array amount" will read (if pay was $1234.56) 001234. Then I was thinking that I could do ifs or cases for each position (hundThous = 0, tenThous = 0, Thous = 1, etc..) to convert it. I'm getting stuck here, though and need help.
How do I put the value 1234 into the char array?
Also, in the function above, I called "checkWriter(money);" , where money is the double. Is that correct? I just want it to call this function to print the converted double to words.
void checkWriter(double z) { double v; int w, y, cents; int b, c, x, length; char array[SIZE]; char amount[SIZE]; /*size = 7, our largest value will be in the hundred thousands*/ v = 100 * z; /*make z a whole number*/ w = ((int)v); /*convert z to an integer w*/ cents = w % 100; /*cents equals the remainder of w/100*/ y = (w - cents)/100; /*y equals the converted integer minus cents, divided by 100*/ sprintf(array, "%-6d", y); /* ATTEMPTING TO PUT y INTO array (saw this on google) */ printf("%s\n\n", array); /* Just wanted to see if it worked. It didn't. I got -2big number.*/ length = strnlen(array); /*find length of the value in array*/ array[length] = '\0'; /* affix terminating null character to array */ b = SIZE - length - 1; /* b is amount of zeroes needed */ for(c = 0; c < length - 1; c++) { /* loops, plugging zeroes in amount until b=c, then attaches array to amount */ if(b == c) { amount[c] = '\0'; strcat(amount, array); } else { amount[c] = '0'; } } printf("%s\n\n", amount); /* Checking to see if it worked. Nope. All zeroes. And sometimes extra symbols at the end*/ return; } Would really appreciate help. Thank you!!!