0

Other than that I feel like an idiot, I'm having problems with my code for CS50's Get Cash program. There may be MANY errors still waiting, but currently the only error has to do with the sum of the 'remaining coins' in printf. (Program asks for user input as to an amount to give in change, converts it from float to int - and then calculates the least amount of coins to give as change)

First, I tried to share the printf with text and then the equation giving the sum of four integers following it. I thought I could add the integers and have the sum printed in printf - but no.

Second, I initialized 'sum' as an integer and did the math on a separate line - but when I tried to add sum after the text in printf, I would get this error:

error: data argument not used by format string

Which pointed at the printf line:

printf("Total Number of Coins for change:\n", sum); ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ^

Here is the code:

/* this program is designed to give cashier the minimum number of coins to give as change after a purchase is made*/

include

include

int main(void) { float n = 0; int q, d, ni; int remd, remni, remp;

/* Require user input a number (float) representing an amount of change in coins to be returned. Number must be positive*/

do { printf("Enter amound of change to be returned: $"); n = get_float(); } while (n > 0);

/* The following code rounds off float 'n' to two digits and convert it to pennies from dollars and cents. Then it is converted into an integer*/

int i = n * 100;

/* The following are loops that will take integer 'i' and subtract first: quarters (int 'q'), second: dimes (int 'd' from the remainder) and third: nickels (int 'ni') and add them to the final remainder, which is the amount of pennies left, thus giving the least number of coins to give for change*/

while (i > 24) { q = i / 25; remd = i % 25; } while (remd > 9) { d = remd / 10; remni = remd % 10; } while (remni > 4) { ni = remni / 5; remp = remni % 5; }

/* finally - print the total number of coins to be given as change*/

/first, get the sum of the coins/ int sum=(q+d+ni+remp);

printf("Total Number of Coins for change:\n", sum);

return 0; }

Thanks so much for the help. I'm really trying to get a hang of this, but without an actual person to ask and bounce ideas off of, it is proving very difficult.

-singer

0

You give printf a format string and a variable. But since your format string does not contain a placeholder like %i (for integer value), it doesn't know what to do with the value.

You should ask for the value until result is non-negative, so repeat as long as value is negative, not as long as it is positive.

BTW, no need for the while, and they don't work as expected either, as the value tested does not change in the loop, you'll run into infinite loops. You mean if, but even that is not required, and the way you wrote the code it would mean certain variables might not be assigned to, but used later (which means result is undefined and could be anything). Later you'll meet valgrind, which is a tool that can detect certain forms of using undefined values. For now, make sure that whichever data you enter, all your variables are at a defined value.

  • thank you for your reply! Ok - one more thing, if I may - You mentioned in your first paragraph: "You give printf a format string and a variable. But since your format string does not contain a placeholder like %i (for integer value), it doesn't know what to do with the value." My code was: int sum=(q+d+ni+remp); printf("Total Number of Coins for change:\n", sum); I thought that since sum is an int, it would print 'Total...' followed by int sum. I have searched and tried to learn about % and how it works. If you have a good source, I'd love that. Thanks – OperaSinger Mar 29 '18 at 15:14
  • Did you know there's a wikipedia entry "printf format string"? And printf does not just print all the values you present it with, but it uses the first argument, the "format string", to determine how to present the various values. – Blauelf Mar 29 '18 at 15:44

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .