-1

I am still having trouble getting past greedy! Not sure why I'm still getting hung up after the prompt. It feels like an infinite loop, which I tried fixing after some earlier advice. check50 shows no errors at this point.

#include <stdio.h>
#include <cs50.h>
#include <math.h>

int main(void)
{
int input;
int coins = 0;
int counter1 = 0;
int counter2 = 0;
int counter3 =0;
int counter4 = 0;
int quarters, dimes, nickels, pennies;
int remaining = 0;


 printf("O hai! How much change is owed?");
 input = GetFloat();
 int cents = round(input * 100);

   for ( ; cents >= 25; cents--)
{
   quarters = (cents/25);
   counter1 = quarters;
   remaining = (cents % quarters);
}

  for ( ;remaining >= 10 || remaining <= 24; remaining--)
{
   dimes = (remaining/10);
   counter2 = dimes;
   remaining = (remaining % 10);
}

for ( ; remaining >= 5 || remaining <= 9; remaining--)
{
   nickels = (remaining/5);
   counter3 = nickels;
   remaining = (remaining % 5);
}

 for ( ; remaining >=1 || remaining <= 4; remaining--)
{
   pennies = (remaining/1);
   counter4 = pennies;
   remaining = (remaining % 1 );
}

coins = (counter1 + counter2 + counter3 + counter4);

 printf("%d.2\n", coins);

}
1
  • Please don't open a new question just to bring attention to a problem you have already asked about. – Air Dec 2 '14 at 18:18
0

Pick an arbitrary value to test your code. Run it through your first loop (the one with quarters), you'll see that it makes no sense. Rather than quarters=cents/25, you might want to try just subtracting 25, keep track of the remainder AND the number of times you've looped (=number of quarters), and loop until the remainder is < 25. As it is, you're doing neither. Ditto with the rest of the denomination loops.

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