can I use scanf to get input (some times user enters input sometimes not, just hit keyboard)?

Discussion in 'C Programming' started by santa19992000@yahoo.com, Sep 8, 2005.

  1. Guest

    can I use scanf to get input (some times user enters input sometimes
    not, just hit keyboard)?.
    It displays to enter IP address, if user wants to change, then he
    enters, otherwise he hits keyboard, which should prompt next one, how
    can I do in C program?. Thanks.

    enter IP address:
    , Sep 8, 2005
    #1
    1. Advertising

  2. wrote:
    >can I use scanf to get input (some times user enters input sometimes
    >not, just hit keyboard)?.
    >It displays to enter IP address, if user wants to change, then he
    >enters, otherwise he hits keyboard, which should prompt next one, how
    >can I do in C program?. Thanks.


    I'd just fetch input with fgets, then analyze it and decide
    what to do next.
    --
    Irrwahn

    Message sent by hitting the keyboard.
    Irrwahn Grausewitz, Sep 8, 2005
    #2
    1. Advertising

  3. In article <>,
    <> wrote:
    >can I use scanf to get input (some times user enters input sometimes
    >not, just hit keyboard)?.
    >It displays to enter IP address, if user wants to change, then he
    >enters, otherwise he hits keyboard, which should prompt next one, how
    >can I do in C program?. Thanks.


    If you want execution to proceed without the user having to press
    enter, then there is no portable method to do what you want in C.
    The non-portable methods are OS specific, and are topics appropriate
    for newsgroups specific to your [unnamed] OS.
    --
    "[...] it's all part of one's right to be publicly stupid." -- Dave Smey
    Walter Roberson, Sep 8, 2005
    #3
  4. Guest

    Guys:

    This is in pure C, I have 3 options to read IP address, netmask and
    gateway address, I need to display in menu, there is one config file
    called addess.cfg, when the system boots, it read that file and
    displays value, it will give options to user incase if he wants to
    modify the value, if he don't want to modify, then he press return key.
    I need to implement in C.

    Thanks.
    , Sep 9, 2005
    #4
  5. In article <>,
    <> wrote:

    >This is in pure C, I have 3 options to read IP address, netmask and
    >gateway address, I need to display in menu, there is one config file
    >called addess.cfg, when the system boots, it read that file and
    >displays value, it will give options to user incase if he wants to
    >modify the value, if he don't want to modify, then he press return key.
    >I need to implement in C.


    Ah, then your difficulty is not to detect that "the keyboard has been hit",
    but rather your difficulty is to detect whether the user input line
    was empty or had some meaningful data.

    There is more than one way to proceed.

    One way would be to read an input line (but NOT using gets()!) into a
    buffer and then use sscanf() to scan the buffer. If there is no
    data value in the string, then you will get either 0 or EOF returned
    by sscanf(). The difficulty with this approach is that you have
    to know what the maximum input line size is ahead of time, or else
    use some mechanism to dynamically allocate the input line buffer as
    you read in characters. Always assume that the user is malicious,
    or at the very least assume that at some point the user will fall
    asleep at the keyboard and end up inputing a looooong string of
    spaces.


    A method to get around the input line size issue is to loop around
    fetching characters (e.g., getchar()), discarding spaces and tabs and
    formfeeds and such, until you either encounter a newline or a
    non-whitespace character.

    - If it was a newline then the empty line was entered and you proceed
    on that case.

    - If the character is not one that can form part of valid input at that
    point, then the user input is in error and you take appropriate action
    to notify the user and flush to the end of the input line.

    - If the character is one that can form part of valid input at that
    point, you ungetc() the character back into the input stream, then
    scanf() using an appropriate format... checking that the format
    actually matched, and checking to be sure nothing extra was entered on
    the input line. [A hint in this last regard: if the user entered valid
    input and it was matched by one of the numeric format specifiers such
    as %d, and if that was the last thing on the line, then the newline
    will be left in the input stream, *not* "consumed" by the scanf().]
    --
    "No one has the right to destroy another person's belief by
    demanding empirical evidence." -- Ann Landers
    Walter Roberson, Sep 9, 2005
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?dGNi?=
    Replies:
    1
    Views:
    756
    Ashish M Bhonkiya
    Apr 27, 2004
  2. =?Utf-8?B?bWF2cmlja18xMDE=?=

    SetAuthCookie works some times and fails some times?

    =?Utf-8?B?bWF2cmlja18xMDE=?=, Mar 23, 2006, in forum: ASP .Net
    Replies:
    0
    Views:
    510
    =?Utf-8?B?bWF2cmlja18xMDE=?=
    Mar 23, 2006
  3. =?Utf-8?B?bWF2cmlja18xMDE=?=

    Forms Authentication Fails some times and not some times???

    =?Utf-8?B?bWF2cmlja18xMDE=?=, Mar 28, 2006, in forum: ASP .Net
    Replies:
    0
    Views:
    488
    =?Utf-8?B?bWF2cmlja18xMDE=?=
    Mar 28, 2006
  4. CK
    Replies:
    9
    Views:
    390
    Jerry Rasmussen
    Oct 19, 2006
  5. Deiva Rajasingam
    Replies:
    3
    Views:
    207
    Deiva Rajasingam
    Jul 29, 2009
Loading...

Share This Page