Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > can I use scanf to get input (some times user enters input sometimes not, just hit keyboard)?

Reply
Thread Tools

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

 
 
santa19992000@yahoo.com
Guest
Posts: n/a
 
      09-08-2005
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:

 
Reply With Quote
 
 
 
 
Irrwahn Grausewitz
Guest
Posts: n/a
 
      09-08-2005
http://www.velocityreviews.com/forums/(E-Mail Removed) 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.
 
Reply With Quote
 
 
 
 
Walter Roberson
Guest
Posts: n/a
 
      09-08-2005
In article <(E-Mail Removed) .com>,
<(E-Mail Removed)> 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
 
Reply With Quote
 
santa19992000@yahoo.com
Guest
Posts: n/a
 
      09-09-2005
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.

 
Reply With Quote
 
Walter Roberson
Guest
Posts: n/a
 
      09-09-2005
In article <(E-Mail Removed) .com>,
<(E-Mail Removed)> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Ignore F1-F12 keys when user enters text input on console Deiva Rajasingam Ruby 3 07-29-2009 06:21 AM
Can't hit the site by name but I can hit by IP CK ASP .Net 9 10-19-2006 08:01 PM
difference between scanf("%i") and scanf("%d") ??? perhaps bug inVS2005? =?ISO-8859-1?Q?Martin_J=F8rgensen?= C Programming 18 05-02-2006 10:53 AM
scanf (yes/no) - doesn't work + deprecation errors scanf, fopen etc. =?ISO-8859-1?Q?Martin_J=F8rgensen?= C Programming 185 04-03-2006 02:49 PM
RangeValidator - not working when user enters a space =?Utf-8?B?dGNi?= ASP .Net 1 04-27-2004 04:58 AM



Advertisments