[comp.lang.c] Neophyte seeks scanf

chandler@kinetics.com (Mark Chandler) (03/06/91)

The News Manager)
Nntp-Posting-Host: plasma
Organization: Novell, Walnut Creek CA.
Distribution: na
Date: Mon, 4 Mar 1991 23:31:41 GMT

I just started a C programming class a few weeks ago and
have run into a problem that I can't figure out.  I have
a *very* simple funtion that prompts the user to input a
number and then echoes it back.  The function is:

float get_tax(void)
{
         float tax;
	
	  printf("Enter the tax: ");
	  scanf("%f", &tax);
	  printf("The tax is %.4f\n", tax);
	
	  return tax;
}

Running it produces the following:

Enter the tax: 0.01
The tax is 0.1000
           ^^^^^^

Can someone explain what's going on here?  I'm running
THINK C v4.02 with the TCL, scanf(), and color.h updates
on a Mac IIsi.

*Any* help is greatly appreciated.



Mark Chandler              |     The opinions expressed above 
chandler@wc.novell.com     |     are mine, not my employer's.

dsebbo@dahlia.uwaterloo.ca (David Ebbo) (03/09/91)

>From: chandler@kinetics.com (Mark Chandler)
>
>float get_tax(void)
>{
>         float tax;
>	
>	  printf("Enter the tax: ");
>	  scanf("%f", &tax);
>	  printf("The tax is %.4f\n", tax);
>	
>	  return tax;
>}
>
>Running it produces the following:
>
>Enter the tax: 0.01
>The tax is 0.1000
>           ^^^^^^

That looks like a bug in your compiler, since your code is fine.
I tried it on gcc, and it worked fine (i.e. The tax is 0.0100).

David Ebbo.

huang@husc8.harvard.edu (Howard Huang) (03/12/91)

   >float get_tax(void)
   >{
   >         float tax;
   >	
   >	  printf("Enter the tax: ");
   >	  scanf("%f", &tax);
   >	  printf("The tax is %.4f\n", tax);
   >	
   >	  return tax;
   >}
   >
   >Running it produces the following:
   >Enter the tax: 0.01
   >The tax is 0.1000

   That looks like a bug in your compiler...

Some earlier versions of Symantec's THINK C have this funny scanf
bug.  I'm sure you could call Symantec and ask them how to fix the
bug, or maybe you can find something on one of the Mac ftp sites
(sumex.stanford.edu).

--

----------------------------------------------------------------------------
Howard C. Huang                        Internet:  huang@husc9.harvard.edu
Junior Computer Science Major          Bitnet:    huang@husc9.BITNET
Mather House 030, Harvard College      UUCP:      huang@husc9.UUCP (I think)
Cambridge, MA 02138                    Apple II:  ftp husc6.harvard.edu