I finally got my change program to print out some numbers instead of throwing a floating point exception. However, now it is slightly off. Instead of giving 2 quarters, 1 dime, 1 nickel, and 2 pennies, the program only selects one penny and I cannot figure out why from any inspection on the conditionals or structure of the code. I added some checks in each if statement but not even that is printing out, I am incredibly confused now..
#include <stdio.h>
#include <cs50.h>
#include <math.h>
int changeF(float change, float div)
{
int c = change/div;
return c;
}
int main(void)
{
printf("Enter the bill amount:");
float bill = GetFloat();
printf("Enter the payment amount:");
float payment = GetFloat();
float q = .25; float di = .1; float n = .05; float p = .01;
float change = payment - bill;
do
{
if (bill >= 0.0 && payment >= 0.0 && change >= 0.0)
{
if (change >= q)
{
int quarters = changeF(change, q);
printf("%d quarters\n", quarters);
change = change - (.25*quarters);
}
if (change >= di)
{
int dimes = changeF(change, di);
printf("%d dimes\n", dimes);
change = change - (.1*dimes);
}
if (change >= n)
{
int nickels = changeF(change, n);
printf("%d nickels\n", nickels);
change = change - (.05*nickels);
printf("%f change", change);
}
if (change >= p)
{
int pennies = changeF(change, p);
printf("%d pennies\n", pennies);
change = change - (.01*pennies);
printf("%f", change);
}
}
}
while (change > 0.0);
}
Try again by appending a newline at the end of the string:
Reason:
stdoutis line buffered, and you'll need to give it a newline to flush the output.Or explicitly flush it by
fflush(stdout);.