473,769 Members | 3,923 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 3875
sa***********@y ahoo.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************ **********@g14g 2000cwa.googleg roups.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************ **********@g49g 2000cwa.googleg roups.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
3467
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 experiment 3 would give some kind of weird error, due to the lack of a space between the two `percent d's in the scanf statement, but it didn't. Experiment 1: ###########################
12
9870
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. However no explanation is offered other than that scanf handels end of lines very badly. I have exeperienced such problems when doing some numerical programming but never understood it. Things like some consequitive scanfs would not read in values...
20
2919
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 is the number of printed characters; so if I could somehow get my year variable to store the leading zeros, I could just run a check: int dummy = 0; dummy = printf("%d", year);
7
7665
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 scanf cannot fill any fields it returns EOF. I have run some tests on scanf and, so far, I've not found an EOF return. For example: If the programer formats scanf for an int or
6
599
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 just a representation, when I print each character in the two char arrays, Key1 displays ok but Ptext is missing the first element. I'm stumped can someone please help. The following compiles and runs to show my problem. Thanks in advance. ...
33
3166
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 help me in finding why such thing is happening and what the remedy to it is. Kindly bear with my English. int main ()
6
26293
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 non-number, he/she will be asked to retry. /* start */ int n;
8
5171
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 discarding any more than needed. (In the best case, when scanf() correctly works and the user hasn't input spurious data beside those required in the control string, the loop will only iterate once.)
4
5073
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
10216
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10049
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9997
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9865
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7413
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5309
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5448
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3965
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2815
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.