[Courses] [C] help me find my C bug? (long)

Suzi Anvin suzi at anvin.org
Tue Jul 9 15:57:18 EST 2002


I can send files if needed, but won't send unless you request...  I'm 
writing Exercise 6-4 in the book recc'ed on the courses website 
(practical C programming) and of course doing a few fancy things with 
it.  It has you input a number in dollars and cents (0.00) and then 
tells you how to make change.  It seems to work with a few funny bugs in 
the output (set off by ***):

Enter the amount of money to make change for: $87.65
To make change for $87.65, you need:
87 dollars
2 quarters
1 dime
1 nickel
Enter the amount of money to make change for: $0.26
To make change for $0.26, you need:
***1 quarter
Enter the amount of money to make change for: $0.46
To make change for $0.46, you need:
1 quarter
2 dimes
1 penny
Enter the amount of money to make change for: $0.31
To make change for $0.31, you need:
1 quarter
1 nickel
1 penny
Enter the amount of money to make change for: $1.26
To make change for $1.26, you need:
***1 dollar
***1 quarter
Enter the amount of money to make change for: $.51
To make change for $0.51, you need:
***2 quarters
Enter the amount of money to make change for: $.27
To make change for $0.27, you need:
1 quarter
2 pennies
Enter the amount of money to make change for: $1.01
To make change for $1.01, you need:
***1 dollar

Basically, ONLY when there is one penny and no dimes or nickels, it 
doesn't print that there is one penny left!  and I have no clue why this 
might be.  Here is the text of the code:

#include <stdio.h>

char line[50];
float amount;			/* the amount of $ as entered by the user */
int total;			/* amount * 100, converted to an interger */
int dollars;			/* # of dollars needed to make change */
int quart;			/* # of quarters needed */
int dime;			/* # of dimes needed, etc. */
int nick;
int penn;

int main()
{
   while (1) {
     /* inputting dollar amount */
     printf("Enter the amount of money to make change for: $");
     fgets(line, sizeof(line), stdin);
     if (sscanf(line, "%f", &amount) <= 0)
       break;
     while (amount <= 0) {
       printf("Please enter an amount greater than 0: $");
       fgets(line, sizeof(line), stdin);
       if (sscanf(line, "%f", &amount) <= 0)
          break;
     }
     total = amount * 100;

     /* determining how many of each coin is needed */
     dollars = total / 100;
     total %= 100;
     quart = total / 25;
     total %= 25;
     dime = total / 10;
     total %= 10;
     nick = total / 5;
     penn = total % 5;

     /* printing the change needed.  Program should only print a type of
      * coin if it is needed to make change (i.e. it should not print
      * "0 quarters".                                                  */
     printf("To make change for $%.2f, you need:\n", amount);
     if (dollars > 1)
       printf("%d dollars\n", dollars);
     if (dollars == 1)
       printf("%d dollar\n", dollars);
     if (quart > 1)
       printf("%d quarters\n", quart);
     if (quart == 1)
       printf("%d quarter\n", quart);
     if (dime > 1)
       printf("%d dimes\n", dime);
     if (dime == 1)
       printf("%d dime\n", dime);
     if (nick == 1)
       printf("%d nickel\n", nick);
     if (penn > 1)
       printf("%d pennies\n", penn);
     if (penn == 1)
       printf("%d penny\n", penn);
   }
   return (0);
}




More information about the Courses mailing list