# PSet1 - Coins issue

While doing the Coins problem set, I am receiving an error while trying to compile and I can't figure out what the issue is.

"cash.c:53:27: error: variable 'cents' used in loop condition modified in loop body"

How I'm approaching the calculate_quarters function (and same for the ones below for dimes, nickels, and pennies) is by using a for loop.

• Initialized quarter = 0
• As long as cents greater than 25
• Increment quarter by 1

Within {}, I have cents = cents - 25 to have cents be whatever remains after the loop breaks and there are no more quarters to give.

I can see that there's an issue with the expr cents, but I cannot figure out what I'm doing wrong here. Is there a better way to code this? Am I just approaching it all wrong?

There's a semi-colon after your for loop, so it's simply ignoring the body. Remove that and you should be good.

You can do this without a loop. Think about how you would approach this if you were to solve this by hand with a calculator. How many times does 25 go in the number of cents you have? By hand you might simply take the number of cents and divide that by 25, which would then be the number of quarters. The remaining cents are the remainder after this division.

• Thanks for your help here. I'm just having a hard time understanding how this would be done without a loop in the main function definition. I still have a hard time knowing how to write out the function definitions so even though I can do it by hand, it's hard for me to put it into code if that makes sense... Commented Jan 29, 2022 at 21:59
• @CalvinLee I mean you could simply return cents divided by 25. That's the only thing you need to do for this function. Commented Jan 31, 2022 at 7:15

You also have the problem that it will always produce 0 as a result because of this:

``````return 0;
``````

``````return quarter;
• No, it wouldn't. The return statement will return the single value that follows the word "return", or the contents of a variable. Examples: `return 33;` would return the value 33. `return coincount;` would return whatever value is stored in coincount. Also, values and variables used inside the function are not visible to main, so any other variables and their contents are lost when the function terminates. You should review the class material on returning values from functions, or google the subject. Commented Jan 30, 2022 at 18:00