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"

enter image description here

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?

2 Answers 2


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...
    – Calvin Lee
    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;

Instead, it should

return quarter;

That still leaves the problem of leaving the correct amount of change to calculate the dimes and nickels. Remember, function calls pass values by copy, not by variable. That means that "change" in a function is not the same as in main. If you alter "change" in a function, the value stored in "change" in main remains unchanged. (No pun intended.)

  • Thank you for your help! Wouldn't return 0 return the value/integer here? Perhaps I'm just not understanding it very well but that's why I left it as "return 0", even for the other parts of the pset (dime, nickel, pennies).
    – Calvin Lee
    Commented Jan 29, 2022 at 21:57
  • 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.
    – Cliff B
    Commented Jan 30, 2022 at 18:00
  • Of course, you're still very early in the course. Kudos for trying to use functions already, but a lot of this will be covered in a few weeks. Also, the topic of scope will relate to this too.
    – Cliff B
    Commented Jan 30, 2022 at 18:03

You must log in to answer this question.

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