After 2 days of getting the same result with pset1 greedy I am a bit desperate here to figure out what I am missing.
My pset1 greedy compiles properly but it doesn't yield correct results. I ran check50 and it seems like it only counts quarters correctly and doesn't add other coin counts to it.
Here is what I also tried, but with the same exact result:
- having different integers for each coin denomination and then adding them all at the end
- having separate names for leftover change after each of the denominations was counted
- after checking for quarters, formulating the conditions as e.g., less than 25 and more than or equal to 10.
- any combination of the above
I suspect there is something after the counting of quarters that I am not getting right. I have rewatched many shorts, walkthroughs and I can't find what else it is that I am getting wrong.
I would love it if someone could point me to the core of my problem!! Thanks so much for your time.
PS: I am not including the beginning of the code that has libraries and int main(void).
{ float change;
// ask how much change
do
{
printf("How much change? ");
change = GetFloat();
}
while(change <= 0);
int change_cents = 0;
int leftover = 0;
int coins = 0;
change_cents = round(change * 100);
// if user gives us number larger than or equal to quarter
// calculcate integer = how many quarters are in the change
// then calculate leftover
if (change_cents >= 25)
{
coins = change_cents / 25;
leftover = change_cents % 25;
}
// if leftover is larger than or equal to a dime
// calculate how many dimes are in the change
// calculate leftover
else if (leftover >= 10)
{
coins = coins + (leftover / 10);
leftover = leftover % 10;
}
// if leftover is larger than or equal to nickel
// calculate how many nickels - there could be only 1 max
// calculate leftover
else if (leftover >= 5)
{
coins = coins + (leftover / 5);
leftover = leftover % 5;
}
// leftover is equal to number of pennies
else
{
coins = leftover;
}
//print the number of total coins
printf("%d\n", coins);
}