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

Jenn Vesperman jenn at anthill.echidna.id.au
Wed Jul 10 12:15:22 EST 2002


On Wed, 2002-07-10 at 12:03, Mary wrote:

> > 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;
> 
> OK, let's insert a line here to check that we're getting the right
> total. We expect the total to be 101.
> 
> When I add a line saying:
>         fprintf (stderr, "Check #1: is total correct?\n\ttotal=%d\n\n", total);
> 
>         /* you could use a standard printf here too, I'm choosing to
>          * write to standard error rather than standard out like printf
>          * would */
> 
> I get:
>         Check #1: is total correct?
>                 total=100
> 
> so that's WRONG.

heh. I just spotted the problem! (proud of myself now.)

Problem and explanation available on request, but I think Mary provided
a link to the explanation.




Jenn V.
-- 
    "Do you ever wonder if there's a whole section of geek culture 
        	you miss out on by being a geek?" - Dancer.

jenn at anthill.echidna.id.au     http://anthill.echidna.id.au/~jenn/





More information about the Courses mailing list