Hello everyone, I am studying the programming language C from the scratches, so my question might be too simple for you guys:
How would you clear your previous keyboard strokes while let the program still run?
I did some research online, and yeah, some guy said, just use the
But, it kills everything in the keyboard buffer(excuse me if my understanding is wrong), which is not what I want.
Say, you got a prompt, letting you to input some info, the program must run as a loop waiting for the next input. If the input was invalid, then it must clear all the previous "unread" characters.
I checked with the <stdio.h> and <string.h> libs, but couldn't find a proper answer...
5 7510
fflush() is the only option. You can use it conditionally when input is invalid. For more help you can show your program.
fflush() is the only option. You can use it conditionally when input is invalid. For more help you can show your program.
OKay, conceptually talking:
outer loop omitted, which calls the following lengthCheck method
/*to see if there is a '\n' in the first 19 characters, if not, then the input should be cleared and program is ready for the next input.*/ - int lengthCheck(char *test)
-
{
-
int i, temp, flag = 0;
-
for (i=0; i<19; i++)
-
{
-
if (test[i]=='\n')
-
{
-
temp = i;
-
flag = 1;
-
break;
-
}
-
}
-
if (flag == 0)
-
fflush(stdin);
-
-
return temp;
-
}
Or to clarify my puzzled situation, let's look at this problem:
you use fgets(array, 20, stdin); to grab some user input, everything goes fine if the input doesn't exceed the array length. But some careless user stroke more than 20 characters, and awful things happen: once you loop again for the user input, the "unread" characters would bother you.
Actually I tried this in my do-while loop: - fllush(stdin);
-
fgets(array, 20, stdin);
-
fflush(stdin);
It't not working...
Perhaps my understanding of keyboard input is still trivial and novice, but that is really the best effort I can make so far.
Probably you want fflush(stdin). If you have a prompt and the data you recieve is not what you expect, then you are probably better off discarding the entire buffer rather than fish through it trying to figure out what the user has entered.
You need to know few thing about fgets (char* s, n, stdin)
First, it reads (n - 1) characters from stdin. So if your array is of 20 then you should specify 21.
Second, fgets () waits until you press enter not till stdin gets 20 characters from keybord. You are checking for a new line but you will find it only when you enter less then 20 (if you make 21 in fgets call) characters and pressed enter. If you have written more then 20 and then pressed enter, then s won’t have any new line.
You can define delimiters (space/tab) in your string which you enter on keyboard. This way you can break your string into words. First word you can consider as command and later ones as options or data.
Sign in to post your reply or Sign up for a free account.
Similar topics |
by: Ray |
last post by:
I have English Windows XP Pro and Office 2003 Pro on my computer. When I
enter data into fields of tables, queries and forms of Access 2003, it
automatically switches to Chinese keyboard input. Even though I remove the
Chinese keyboard input from Regional applet, the system will automatically
reinstall Chinese keyboard input when I enter data in Office 2003. After I
switch from Chinese keyboard input back to English keyboard input, it...
|
by: herrcho |
last post by:
What's the difference between STDIN and Keyboard buffer ?
when i get char through scanf, i type in some characters and press
enter,
then, where do the characters go ? to STDIN or Keyboard buffer ? are
they same ?
thanks ^^
|
by: Don Riesbeck Jr. |
last post by:
I'm working on an application (OEM) using C# that utilizes input from a
keyboard, and USB Barcode Scanner. The scanner is a HID Keyboard device,
and input from it is sent to the system as if it were a keyboard. I need to
be able to identify input from the scanner and keyboard independently. I've
looked at DirectX.DirectInput, and using user32.dll to hook into the
keyboard messages, but neither method seems to allow for identification of...
|
by: rs |
last post by:
Hi guys,
I am trying to read from a USB keyboard using vb.net and HID classes. the
USB keyboard is not my primary keyboard. I have a ps2 keyboard connected and
is detected in device manager as my keyboard. the USB keyboard is detected
as HID keyboard device. the program finds the keyboard if it is attached.
and I am getting valid handles. however, everytime I use the readfile
function I am getting "object reference not set to an instant...
|
by: rs |
last post by:
Hi guys,
I am trying to read from a USB keyboard using vb.net and HID classes. the
USB keyboard is not my primary keyboard. I have a ps2 keyboard connected and
is detected in device manager as my keyboard. the USB keyboard is detected
as HID keyboard device. the program finds the keyboard if it is attached.
and I am getting valid handles. however, everytime I use the readfile
function I am getting "object reference not set to an instant...
| |
by: jpierson |
last post by:
Hi,
I am tryin to create a keyboard hook that sends the keystroke ctrl +
pause/break.
I haven't used keyboard hooks before so I'm not too sure how to use
them
public int MyKeyboardProc(int nCode, int wParam, int lParam)
{
|
by: Louis Cypher |
last post by:
I'm working on an application (OEM) using c# that uses input from a keyboard
and a USB Barcode Scanner.
I need to be able to identify keystrokes from the barcode scanner and remove
them from the message queue, regardless of what application has focus.
I can identify the keystrokes and input device by registering for raw input
(RegisterRawInputDevices) and processing the WM_INPUT message. This gives
me the keystrokes and the ability to...
|
by: NaN |
last post by:
I've been trying to use _kbhit() but it didn't do what I thought it
would from books, "Detects whether a keypress is available for reading."
Herbert Schildt says,
"If the user has pressed a key, this function returns
true(non-0), but does not read the character.
If no keystroke is pending, kbhit() returns false (0)."
Here is the test code,
|
by: BD |
last post by:
How can I duplicate the behavior of the operating system shortcut keys
in my application? For example, my windows form has 5 controls
(textboxes), the operating system will pickup which control has the
focus and handle ctrl-c, ctrl-v, or any other shortcuts. I have the
same shortcuts working in my app, but have not determined how to find
out which control has focus. Would I set up a loop or code for each
control at form level. Any help...
|
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
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...
| |
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
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...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |