How do you use double with scanf?

This is a discussion on How do you use double with scanf? within the C Programming forums, part of the General Programming Boards category; Hi, I am having trouble using doubles with scanf. Here is the code i'm using Code: #include <stdio.h> int main() ...

  1. #1
    Registered User
    Join Date
    Jul 2003
    Posts
    49

    Question How do you use double with scanf?

    Hi, I am having trouble using doubles with scanf.

    Here is the code i'm using

    Code:
    #include <stdio.h>
    
    int main()
    {
    	double a = 0;
    
    	printf("enter a number: ");
    	scanf("%lf%", &a);
    	printf("%lf%", a);
    
    		return(0);
    }
    when i run it, i enter say 10 the hit enter. The cursor goes to a newline and nothing happens. I keep hitting enter and it goes to new lines. Then I enter another number then hit enter then it prints 10.

    What is happening?

    Thanks

  2. #2
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    Try taking off the 2nd % sign in the scanf statement, and see if that does anything.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  3. #3
    Registered User
    Join Date
    Jul 2003
    Posts
    49
    woops
    Thanks!!

  4. #4
    Registered User coolshyam's Avatar
    Join Date
    Mar 2005
    Posts
    26
    its fine in a TurboC compiler if you add getch()
    Code:
    #include <stdio.h>
    
    int main()
    {
    	double a = 0;
    
    	printf("enter a number: ");
    	scanf("%lf%", &a);
    	printf("%lf%", a);
    	getch();
    		return(0);
    }

    any questions any type in programming

    a ready made answer
    -----------------------------------------------------------------------------------
    FORTUNE FAVOURS THE BOLD!
    -----------------------------------------------------------------------------------

  5. #5
    Registered User
    Join Date
    Mar 2004
    Posts
    494
    That will give you an error coolshyam. Get a new compiler, in every reply/post you have made so far you always mention Turbo C compiler. Try this http://www.bloodshed.net/devcpp.html

    edit: for some reason Devc++ gives no errors or warnings, but Visual C++ considers getch as an undeclared identifier. Still XSquared is right, you should remove the 2nd %.
    Last edited by InvariantLoop; 03-14-2005 at 08:45 AM.
    When no one helps you out. Call google();

  6. #6
    Registered User coolshyam's Avatar
    Join Date
    Mar 2005
    Posts
    26

    Smile

    Quote Originally Posted by InvariantLoop
    That will give you an error coolshyam. Get a new compiler, in every reply/post you have made so far you always mention Turbo C compiler. Try this http://www.bloodshed.net/devcpp.html

    edit: for some reason Devc++ gives no errors or warnings, but Visual C++ considers getch as an undeclared identifier. Still XSquared is right, you should remove the 2nd %.

    yes i do realize that most of you buddies use dev C++. but i dont own a computer. i use my college computer and the admin dosent allow me to download devc++. no probs.i shall not use getch() any more.atleast the logic will be interpreted in the same way in both compilers isnt that so? but this summer im getting my own computer and rest assured, i will download dev c++.

    any questions any type in programming

    a ready made answer
    -----------------------------------------------------------------------------------
    FORTUNE FAVOURS THE BOLD!
    -----------------------------------------------------------------------------------

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Testing some code, lots of errors...
    By Sparrowhawk in forum C Programming
    Replies: 48
    Last Post: 12-15-2008, 03:09 AM
  2. Rectangular Approximation Program Help
    By Noah in forum C Programming
    Replies: 4
    Last Post: 03-15-2006, 01:23 PM
  3. Unknown Math Issues.
    By Sir Andus in forum C++ Programming
    Replies: 1
    Last Post: 03-06-2006, 05:54 PM
  4. Please HELP!!
    By traz in forum C++ Programming
    Replies: 4
    Last Post: 04-14-2003, 09:20 PM
  5. what's the difference?
    By iluvmyafboys in forum C++ Programming
    Replies: 13
    Last Post: 02-28-2002, 08:25 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21