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

Lorne Gutz lgutz at vistar.ca
Wed Jul 10 10:03:24 EST 2002


See below for another solution, may not be the best.


  if (sscanf(line, "%f", &amount) <= 0)

If you take this line and change it to use two ints rather than
one float the problem goes away.

  if( sscanf( line, "%d." "%d",  &dollars, &cents ) <= 0 )

define dollars and cents as ints 
You will have to make sure that there is a zero or some integer
value for dollars.  Attempting to convert a float to an int is
compiler dependent, and weird things can happen.

cheers
Lorne

On Wednesday 10 July 2002 00:12, Suzi Anvin wrote:
> 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;




More information about the Courses mailing list