Coding a program that allows user to input a cash amount up to $200.00 and then computes and prints its value in the following denominations (20, 10, 5, 1, .25, .10, .05, .01). I think I've figured out the basics of how to get the denominations (division/modulus), but it's the structure of the do/while and if/else that's giving me trouble. I keep getting an error that I need a while statement even though I've entered it and its condition (seen below), but I also am at a bit of a loss as to where to put a range prompt (if the user inputs something negative or above 200). Any suggestions/guidance would be greatly appreciated!
double amt_ent;
int twenty, ten, five, one, quarter, dime, nickel, penny, remainder;
printf ("Enter a dollar amount up to $200.00:");
scanf ("%lf", &amt_ent);
do
{ printf ("Name - Assignment 2 - Change-O-Matic\n");
printf ("Amount entered: $%.2lf\n", ((amt_ent*100)/100));
printf ("Change breakdown:\n");
{ /*Change in twenties*/
twenty= (int) amt_ent/20;
if (twenty >= 2)
printf("%i\t$20.00s\n", twenty);
if (twenty == 1)
printf ("%i\t$20.00\n", twenty);
else
/*Change in tens*/
remainder = twenty % 20;
ten = remainder/10;
if (ten >=2)
printf ("%i\t$10.00s\n", ten);
if (ten == 1)
printf ("%i\t$10.00\n", ten);
else
/*Change in fives*/
remainder = ten % 10;
five = remainder/10;
if (five >= 2)
printf ("%i\t$5.00s\n", five);
if (five == 1)
printf ("%i\t$5.00\n", five);
else
/*Change in ones*/
remainder = five % 5;
one = remainder/1;
if (one >= 2)
printf ("%i\t$1.00s\n", one);
if (one == 1)
printf ("%i\t$1.00\n", one);
else
/*Change in quarters*/
remainder = one % 1;
quarter = remainder/.25;
if (quarter >= 2)
printf ("%i\t$.25s\n", quarter);
if (quarter == 1)
printf ("%i\t$.25\n", quarter);
else
/*Change in dimes*/
remainder = quarter % 4;
dime = remainder/.10;
if (dime >= 2)
printf ("%i\t$.10s\n", dime);
if (dime == 1)
printf ("%i\t$.10\n", dime);
else
/*Change in nickels*/
remainder = dime % 10;
nickel = remainder/.05;
if (nickel >= 2)
printf ("%i\t$.05s\n", nickel);
if (nickel == 1)
printf ("%i\t$.05\n", nickel);
else
/*Change in pennies*/
remainder = nickel % 20;
penny = remainder/100;
if (penny >= 2)
printf ("%i\t$.01s\n", penny);
if (penny == 1)
printf ("%i\t$.01\n", penny);
}
while ((amt_ent <= 200.00) && (amt_ent >= 00.00));}
return 0;
Try this... Tested and it works. You problem was in the way reminder was being calculated. You need to divide the amt_ent again with previous step. Again all % operations work only with integers. So you need to convert your double to integer domain by multiplying with 100 before proceeding with calculations.
As regarding you error, there is extra brace present after
printf ("Change breakdown:\n");
. And you need not to put closing brace after while statement.while ((amt_ent <= 200.00) && (amt_ent >= 00.00));}
Remove that also.For you valid amount handling problem, there is a
continue
command which skips the remaining loop and reiterate from beginning when encountered. You can use that.If amount entered is invalid, the
continue
statement will execute and remains loop is skipped. Thenprintf ("Enter a dollar amount up to $200.00:");
will be executed. So you can see, user wont be able to go ahead of continue unless he enters correct value of amount.