问题
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;
回答1:
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.
do
{
printf ("Enter a dollar amount up to $200.00:");
scanf ("%lf", &amt_ent);
if(amount_ent < 00.00 || amount_ent>200.00)
continue;
printf ("Name - Assignment 2 - Change-O-Matic\n");
printf ("Amount entered: $%.2lf\n", ((amt_ent*100)/100));
printf ("Change breakdown:\n");
If amount entered is invalid, the continue
statement will execute and remains loop is skipped. Then printf ("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.
回答2:
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.
#include<stdio.h>
#include<stdlib.h>
main()
{
double amt_ent1;
int amt_ent;
int twenty, ten, five, one, quarter, dime, nickel, penny;
do
{
printf ("Enter a dollar amount up to $200.00:"); //<== it is put in due to get the statement again else it execute with the same value.
scanf ("%lf", &amt_ent1);
amt_ent = (amt_ent1*100)/100;
printf ("Name - Assignment 2 - Change-O-Matic\n");
printf ("Amount entered: $%.2lf\n", amt_ent1);
printf ("Change breakdown:\n");
if ((amt_ent > 200.00) || (amt_ent < 00.00)) continue;
amt_ent = amt_ent1 * 100;
{ /*Change in twenties*/
twenty= (int) amt_ent/2000;
if (twenty >= 2)
printf("%i\t$20.00s\n", twenty);
if (twenty == 1)
printf ("%i\t$20.00\n", twenty);
/*Change in tens*/
amt_ent = amt_ent % 2000;
ten = amt_ent/1000;
if (ten >=2)
printf ("%i\t$10.00s\n", ten);
if (ten == 1)
printf ("%i\t$10.00\n", ten);
/*Change in fives*/
amt_ent = amt_ent % 1000;
five = amt_ent/500;
if (five >= 2)
printf ("%i\t$5.00s\n", five);
if (five == 1)
printf ("%i\t$5.00\n", five);
/*Change in ones*/
amt_ent = amt_ent % 500;
one = amt_ent/100;
if (one >= 2)
printf ("%i\t$1.00s\n", one);
if (one == 1)
printf ("%i\t$1.00\n", one);
/*Change in quarters*/
amt_ent = amt_ent % 100;
quarter = amt_ent/25;
if (quarter >= 2)
printf ("%i\t$.25s\n", quarter);
if (quarter == 1)
printf ("%i\t$.25\n", quarter);
/*Change in dimes*/
amt_ent = amt_ent % 25;
dime = amt_ent/10;
if (dime >= 2)
printf ("%i\t$.10s\n", dime);
if (dime == 1)
printf ("%i\t$.10\n", dime);
/*Change in nickels*/
amt_ent = amt_ent % 10;
nickel = amt_ent/5;
if (nickel >= 2)
printf ("%i\t$.05s\n", nickel);
if (nickel == 1)
printf ("%i\t$.05\n", nickel);
/*Change in pennies*/
amt_ent = amt_ent % 5;
penny = amt_ent/1;
if (penny >= 2)
printf ("%i\t$.01s\n", penny);
if (penny == 1)
printf ("%i\t$.01\n", penny);
}
}
while ((amt_ent <= 2000) && (amt_ent >= 0)); //<== it is put inside the do loop it is wrong, it come outside the do lopp.
return 0;
}
来源:https://stackoverflow.com/questions/22032680/cash-change-program-using-loops-and-if-else