Reading an input from the keyboard

This is a discussion on Reading an input from the keyboard within the C Programming forums, part of the General Programming Boards category; I have a program that outputs the following: Code: $ ./a.out Enter a number: 3 the number you entered is: ...

  1. #1
    Registered User
    Join Date
    Aug 2005
    Posts
    194

    Reading an input from the keyboard

    I have a program that outputs the following:
    Code:
    $ ./a.out
    Enter a number:
    3
    the number you entered is:
    4.85462e-270
    $
    Now, clearly I did not enter the number 4.85462e-270. Below is the code that produced this error.
    Code:
    #include <stdio.h>
    
    int main()
    {
    double x;
    
    printf ("Enter a number:\n");
    scanf("%g", &x);
    printf("the number you entered is:\n%g\n",x);
    
    return 0;
    }
    Where is my error? How do I fix it?

  2. #2
    @codeguru
    Join Date
    Jan 2006
    Posts
    34
    did you try "%d" ?

  3. #3
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Quote Originally Posted by Mitsukai
    did you try "%d" ?
    If so, that would be wrong.
    Code:
    scanf("%lf", &x);
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  4. #4
    The Richness... Richie T's Avatar
    Join Date
    Jan 2006
    Location
    Ireland
    Posts
    469

    %g

    %g is a format specifier for printf () only (used to omit trailing zeros from a printed number), it doesn't apply to scanf(), so %lf - long float (double) should work fine

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    long float, which was a synonym for double, was removed in C89.

    printf() doesn't have a %lf, though -- its %f takes double arguments.

    Code:
    #include <stdio.h>
    
    int main(void) {
        double d;
    
        printf("Enter a number: ");
        scanf("%lf", &d);
        printf("You entered %f\n", d);
    
        return 0;
    }
    Several printf() format specifiers have a different meaning for scanf().
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Using threads for control input (keyboard, serial)
    By synthetix in forum C Programming
    Replies: 1
    Last Post: 07-06-2009, 07:43 PM
  2. Replies: 7
    Last Post: 02-02-2009, 06:27 AM
  3. Reading from an input file + processing
    By Mooncow in forum C Programming
    Replies: 2
    Last Post: 12-01-2008, 01:45 AM
  4. About aes
    By gumit in forum C Programming
    Replies: 13
    Last Post: 10-24-2006, 03:42 PM
  5. Help with linked list (and reading user input)
    By p1kn1c in forum C Programming
    Replies: 2
    Last Post: 04-05-2006, 12:43 AM

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