I seem to be having trouble using modulo as conditions inside of while loops.
int main()
{
float n;
do
{
n = get_float("How much change is owed? ");
}
while(n < 0);
int cents = round(n * 100);
int coinsUsed = 0;
while (cents % 10 == 25)
{
cents -= 25;
coinsUsed++;
}
while (cents % 10 == 0)
{
cents -= 25;
coinsUsed++;
printf("%i", coinsUsed);
}
while (cents % 5 == 0)
{
cents -= 5;
coinsUsed++;
}
for (int i = 0; i < cents; i++)
{
coinsUsed++;
}
printf("%i\n", coinsUsed);
}
I try to use a modulo operator to check to see if the number can be divided by 25 for quarters, 10 for dimes, etc. However, when I run the code, I get an overflow error as the while loop continues to operate forever.
while (cents % 10 == 25)
? (and wrong truth test, compare it to the other sentences).