473,287 Members | 1,616 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,287 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 3848
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: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: marcoviolo | last post by:
Dear all, I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...

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.