Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do I put an integer value into a char array? I want the number 1234 to be in the char array. I can't read it from a prompt
    primarykey
    data
    text
    <p>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. </p> <p>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. </p> <p>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. </p> <p>How do I put the value 1234 into the char array?</p> <p>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.</p> <pre><code> 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 &lt; 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; } </code></pre> <p>Would really appreciate help. Thank you!!! </p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload