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

Suzi Anvin suzi at anvin.org
Tue Jul 9 21:12:58 EST 2002


heh.  Hey, I'm just happy the bug was related to something I hadn't 
learned yet.  :)  My chosen fix is to round the sucker in the conversion 
to int:
instead of total = amount * 100
            total = (amount * 100) + 0.5

Cheesy but it works in this instance.  More elegant solutions welcome 
just out of curiosity...
			-Suzi

Jenn Vesperman wrote:

>>>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.





More information about the Courses mailing list