Double problem

This is a discussion on Double problem within the C Programming forums, part of the General Programming Boards category; Hello again. I have problem with double printing. Code: #include <stdio.h> int main() { double a=0; printf("Give a Float number ...

  1. #1
    Registered User
    Join Date
    Apr 2006
    Location
    Larnaca in Cyprus
    Posts
    32

    Double problem

    Hello again. I have problem with double printing.

    Code:
    #include <stdio.h>
    
    int main()
    {
     double a=0;
    
     printf("Give a Float number (ex. 346.58)");
     scanf("%e", &a);
    
     printf("\n\n---->%f\n\n", a);
     
    return 0;
    }
    I run:

    Give a Float number (ex. 346.58)346.58


    ---->0.000000

    Why this and how I can correct it. I want double but I don't want that format 5.609771e-315.

    Thank you again.

  2. #2
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,246
    %e is used for normal sized floating point numbers (float). To enter a double precision number, you need to add a modifier. So it should look like "%le".
    bit∙hub [bit-huhb] n. A source and destination for information.

  3. #3
    Registered User
    Join Date
    Apr 2006
    Location
    Larnaca in Cyprus
    Posts
    32
    Quote Originally Posted by bithub View Post
    %e is used for normal sized floating point numbers (float). To enter a double precision number, you need to add a modifier. So it should look like "%le".
    With %le the result is 5.609771e-315.


    But I want 346.58.

  4. #4
    Epy
    Epy is offline
    Hen-teaser Epy's Avatar
    Join Date
    Sep 2009
    Location
    California, USA
    Posts
    878
    you also have to change the printf code too

    printf and scanf format codes

  5. #5
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,246
    Quote Originally Posted by Epy View Post
    you also have to change the printf code too

    printf and scanf format codes
    No, printf is not the same as scanf. %f is fine for printing doubles.

    I'm not sure what the problem is cs05pp2. I think your code should work with the change to scanf(). What compiler/OS are you using?
    bit∙hub [bit-huhb] n. A source and destination for information.

  6. #6
    Registered User
    Join Date
    Apr 2006
    Location
    Larnaca in Cyprus
    Posts
    32
    Quote Originally Posted by bithub View Post
    No, printf is not the same as scanf. %f is fine for printing doubles.

    I'm not sure what the problem is cs05pp2. I think your code should work with the change to scanf(). What compiler/OS are you using?
    I use two kind: FreeBSD and Fedora

  7. #7
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,246
    Quote Originally Posted by cs05pp2 View Post
    I use two kind: FreeBSD and Fedora
    Neither of those are compilers.
    bit∙hub [bit-huhb] n. A source and destination for information.

  8. #8
    Registered User
    Join Date
    Apr 2006
    Location
    Larnaca in Cyprus
    Posts
    32
    Quote Originally Posted by bithub View Post
    Neither of those are compilers.
    GCC Compiler.

  9. #9
    Registered User
    Join Date
    Dec 2008
    Location
    Black River
    Posts
    128
    I think the actual problem is that %e is used for floating point numbers in exponential form. So if you wanted to input a double the way you're trying now, you should use %lf. Otherwise, with %le, you'd need to type "3.4658e+02"

  10. #10
    Registered User
    Join Date
    Apr 2006
    Location
    Larnaca in Cyprus
    Posts
    32
    Quote Originally Posted by Ronix View Post
    I think the actual problem is that %e is used for floating point numbers in exponential form. So if you wanted to input a double the way you're trying now, you should use %lf. Otherwise, with %le, you'd need to type "3.4658e+02"
    Code:
    #include <stdio.h>
    
    int main()
    {
     double a=0;
     printf("Give a Float number (ex. 346.58)");
     scanf("%e", &a);
    
     printf("\n\n---->%lf\n\n", a);
     
    return 0;
    }
    I try that and I get :

    Code:
    Give a Float number (ex. 346.58)346.58
    
    
    ---->0.000000

  11. #11
    Registered User
    Join Date
    Dec 2008
    Location
    Black River
    Posts
    128
    Right, because:

    - You're using %e when you should be using %le
    - Your input is not in exponential form. Type "3.4658e+02" and see if it works.

    Otherwise, change the %e to %lf and keep the input as is.

  12. #12
    Registered User
    Join Date
    Apr 2006
    Location
    Larnaca in Cyprus
    Posts
    32
    Quote Originally Posted by Ronix View Post
    Right, because:

    - You're using %e when you should be using %le
    - Your input is not in exponential form. Type "3.4658e+02" and see if it works.

    Otherwise, change the %e to %lf and keep the input as is.
    Perfect thank you

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  2. Class problem
    By Lord JoNil in forum C++ Programming
    Replies: 4
    Last Post: 09-22-2006, 05:18 PM
  3. Little Problem on Presentation in the Tower of Hanoi
    By momegao in forum C++ Programming
    Replies: 3
    Last Post: 04-20-2003, 06:22 PM
  4. Please HELP!!
    By traz in forum C++ Programming
    Replies: 4
    Last Post: 04-14-2003, 09:20 PM
  5. getline problem
    By scottmanc in forum C++ Programming
    Replies: 9
    Last Post: 04-13-2003, 09:27 PM

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