Newbie: Trouble with scanf - HELP!!! 
Author Message
 Newbie: Trouble with scanf - HELP!!!

I had trouble with a code i was writing...  the snippet i'm having
trouble with is the following:

int main(void)
{
        double h, r;

        printf("Enter the height and radius: ");
        scanf("%f %f", &h, &r);
        printf("%f %f\n", h, r);

        return 0;

Quote:
}

It works with "int" variables (int h, r; and %d formats)
but it refuses to work with "double"...

I'm not getting an error but i do get junk as output.
someone help!

jim



Wed, 24 Oct 2001 03:00:00 GMT  
 Newbie: Trouble with scanf - HELP!!!

Hi Jim!

If you use double then try to use %lf  instead of %f, because lf means
long float

Quote:

> I had trouble with a code i was writing...  the snippet i'm having
> trouble with is the following:

> int main(void)
> {
>         double h, r;

>         printf("Enter the height and radius: ");
>         scanf("%f %f", &h, &r);
>         printf("%f %f\n", h, r);

>         return 0;
> }

> It works with "int" variables (int h, r; and %d formats)
> but it refuses to work with "double"...

> I'm not getting an error but i do get junk as output.
> someone help!

> jim

  vcard.vcf
< 1K Download


Wed, 24 Oct 2001 03:00:00 GMT  
 Newbie: Trouble with scanf - HELP!!!

Quote:

> I had trouble with a code i was writing...  the snippet i'm having
> trouble with is the following:

> int main(void)
> {
>         double h, r;

>         printf("Enter the height and radius: ");
>         scanf("%f %f", &h, &r);

Apart from the general advice that you should always fflush(stdout) after
prompts not terminated with '\n' and never use scanf() directly,
even if you go the safer fgets()/sscanf() route, the specifier for double in
*scanf() is %lf.

Quote:
>         printf("%f %f\n", h, r);

>         return 0;
> }

> It works with "int" variables (int h, r; and %d formats)
> but it refuses to work with "double"...

> I'm not getting an error but i do get junk as output.
> someone help!

> jim

--




Wed, 24 Oct 2001 03:00:00 GMT  
 Newbie: Trouble with scanf - HELP!!!


Quote:
>This is a multi-part message in MIME format.
>--------------14783CA80A870E50ECCD6773
>Content-Type: text/plain; charset=us-ascii
>Content-Transfer-Encoding: 7bit

>Hi Jim!

>If you use double then try to use %lf  instead of %f, because lf means
>long float

But only in scanf(), %f is correct for double in printf().

--
-----------------------------------------


-----------------------------------------



Wed, 24 Oct 2001 03:00:00 GMT  
 Newbie: Trouble with scanf - HELP!!!
Thanks a bunch for all the replies...

one more question.... I have the second edition of K&R.
Is there a mention to use %lf for scanf() in there??

just curious.  would like to make a note of the page if it exists for
later reference.

thanks.

jim



Thu, 25 Oct 2001 03:00:00 GMT  
 Newbie: Trouble with scanf - HELP!!!

Quote:

>Thanks a bunch for all the replies...

>one more question.... I have the second edition of K&R.
>Is there a mention to use %lf for scanf() in there??

>just curious.  would like to make a note of the page if it exists for
>later reference.

Pages 158 and 245. These can be found simply by looking up scanf in the
index.

--
-----------------------------------------


-----------------------------------------



Thu, 25 Oct 2001 03:00:00 GMT  
 Newbie: Trouble with scanf - HELP!!!


Quote:

> one more question.... I have the second edition of K&R.
> Is there a mention to use %lf for scanf() in there??

In Chapter 7, %lf is shown with scanf. It is just an example, however, and
afaik there is no explicit explanation of why it must be used. The material
on specifiers does not cover the issue.


Thu, 25 Oct 2001 03:00:00 GMT  
 
 [ 8 post ] 

 Relevant Pages 

1. Trouble with scanf() (newbie here)

2. newbie trouble: CoCreateInstance Error + IWebBrowser trouble...

3. TextReader trouble - newbie HELP? :)

4. HELP!! window/viewport trouble - mfc newbie

5. scanf troubles

6. trouble bulltproofing scanf

7. printf/scanf trouble on SPARCstation

8. Troubles using C's scanf Function

9. trouble with scanf?

10. Scanf troubles

11. Trouble with scanf( "...%[^...]", ...)

12. SCANF() NEWBIE

 

 
Powered by phpBB® Forum Software