473,394 Members | 2,048 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

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

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:

Nov 15 '05 #1
4 3855
sa***********@yahoo.com 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.
Nov 15 '05 #2
In article <11**********************@g14g2000cwa.googlegroups .com>,
<sa***********@yahoo.com> 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
Nov 15 '05 #3
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.

Nov 15 '05 #4
In article <11**********************@g49g2000cwa.googlegroups .com>,
<sa***********@yahoo.com> 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
Nov 15 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Steve Zimmerman | last post by:
This post is not intended as an argument to any other post, just some simple scanf experiments that I wanted to share. I found experiments 5 and 6 the most educational. Also, I thought...
12
by: B Thomas | last post by:
Hi, I was reading O'Reilly's "Practical C programming" book and it warns against the use of scanf, suggesting to avoid using it completely . Instead it recomends to use using fgets and sscanf....
20
by: Sivarn | last post by:
I'm writing a program for that takes dates as input using scanf. I want to verify that the user is inputting a full 4 digits for the year. How do I do this? I know that the return value on printf...
7
by: hugo27 | last post by:
obrhy8 June 18, 2004 Most compilers define EOF as -1. I'm just putting my toes in the water with a student's model named Miracle C. The ..h documentation of this compiler does state that when...
6
by: Pete | last post by:
Following is a fragment from a program I'm writing. I am trying to enter an 8 bit binary representation from keyboard which is then scanfed into &Ptext and then again into &Key1. This is not binary...
33
by: Lalatendu Das | last post by:
Dear friends, I am getting a problem in the code while interacting with a nested Do-while loop It is skipping a scanf () function which it should not. I have written the whole code below. Please...
6
by: obdict | last post by:
Hello, I used scanf() in a while loop, which ensures that user input is valid (must be an integer no greater than 21 or less than 3). If user enters a number out of the range, or enters...
8
by: Army1987 | last post by:
Is this a good way to discard unread data after scanf()? while (getchar() != '\n') ; According to the FAQ scanf always leaves the trailing newline on the input stream, so there's no risk of...
4
by: Newbie | last post by:
Hello I need to enter a string of the form abc (a string of characters followed by EOF) #include<stdio.h> #include<stdlib.h> #include<string.h> #define MAX 100 int main(void) {
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.