C Error: declaration shadows a local variable — Won’t let me repeatedly replace the value of my float variable

Replace float cents = with cents = in your while loops.

Currently you’re trying to declare a new variable cents which shadows the existing one. Technically this is valid C, perhaps your compiler (thankfully) has this warning set to an error?

Note that you could optimise much of your logic to O(1) using integer division and careful checking with your debugger. Repeatedly subtracting from a value is crude, can get you into hot water if you work with floating point types, and is unnecessarily computationally expensive. (Although can be a good step to getting things working.)

Finally, do you need that last while loop? Presumably you always want to output the coins dispensed, not an infinite number of times when cents is zero?

Leave a Comment