Hi, I just want to know if is there any way in C to get only 1 input from user?
Like,
Enter a letter: a
After inputting a, should be that I can't anymore type with other letters, only 1.
I'm thinking that there's something in scanf that needs to be modified a little bit.
Thanks.
char letter;
printf("input a letter: ");
scanf("%c", &letter);
10 2947
When you say you allow only 1 input, do you mean
- ignore the rest of the output?
- throw an error if more output comes in?
- after touching a single key, the user's keyboard explodes?
You should use cscanf to allow only 1 input.
cscanf("%c",&char);
Thank you for your replies,
@anurag,
Thank you for that, just a random question, any other way where in I still need to press enter to verify my input?
I think there is no other way.
The answer is: Your program is not running while the uses pounds on the keys. Heaven knows what's been entered. Your program does not regain control until the user presses the enter key-- whenver that is..maybe tomorrow.
Of course, you could write your own keyboard driver and not use the one provided by the C language.
You should get in the habit of using this sequence to prompt for keyboard input: - flush stdin
-
write prompt string to stdout
-
read stdin
That way you can be sure that you are parsing a response that was entered after the prompt rather than something that has been sitting in the input buffer for a few days.
Banfa 9,065
Expert Mod 8TB
I think it is important to point out that you can't actually call the flush routine (fflush) on stdin for example
fflush(stdin); /* Bad */
The standa5rd specifically defines flushing stdin as undefined behaviour.
However (and I am sure Don meant this) you can flush stdin in so far as you can read and discard all the waiting characters before prompting the user for input using normal stdin read methods such as getchar().
Thanks, I didn't know that about fflush(stdin).
If stdin is buffered, then reading til nothing comes out won't actually flush the stream ... any characters entered after the last newline will still be in there.
What's the best way to take care of that? Before I make any other potentially undefined suggestions I did a little research. It says here there is no portable way to empty stdin before printing your annunciator. Ouch!
Any ideas?
what about getch and getchar function. have you tried?
My bad Banfa already answered it.
Only ouput streams can by flushed.
Input streams are not under your control so you can't flush them.
All you can so on stdin is to get characaters until there are no more to get. That means you can't use the scanf family of functions. You will to get a byte at a time and figure out what that byte means in your program.
This is not an easy deal. You will end up writing your own keyboard driver.
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Terry Murray |
last post by:
Hi Everybody,
I have been given the task of creating an online coupon system. The only
real challenge that I can identify at this point is allowing only one coupon
to be printed rather than...
|
by: yjc238 |
last post by:
Here's what needs to be done.
I have a form with email1, email2, email3, email4, and email5.
I need a way to have an alert come up when more than one email field
contains a value. The user can...
|
by: Deano |
last post by:
Here's a good one that keeps defeating me...
I have a continuous form that has a date textbox as one of the controls.
The user can choose any date they like including one they have already...
|
by: Dave Smithz |
last post by:
Hello there,
Summary:
How far can you go with SQL Select queries using like clauses with wildcard
characters. Can you apply anything like regular expressions?
Full details:
On a Intranet...
|
by: sparks |
last post by:
After trying a combo box to do this.
its tied to a lookup table
name value
yes 1
no 0
they wanted to be able to type in a 1 but display a yes in the field.
and store a 1 in the...
|
by: Jesse Engle |
last post by:
how can i allow a user of my program to enter paragraphs seperated by line
spaces using gets() or a similar function?
i want the user to be able to enter as many lines as they want, while able...
|
by: gullyou |
last post by:
Sorry I crossposted this to java group. Didn't know distinction
between Java & Javascript
Hi,
I'm not a Java script programmer but have this web page with a form
form that
includes a...
|
by: Certys |
last post by:
Hello,
I have a form where I only allow new records to be added. I enable
this by setting the form property "Data Entry" to Yes.
I want to access other records in the same table- to autofill...
|
by: Amzul |
last post by:
hello all,
i have a form with 6 elemnt:
<label><input type="radio" name="name_b" value="120x60" checked="checked"/>120x60</label><!-- #1 -->
<input type="radio" name="name_b" value="468x60"...
|
by: ajd335 |
last post by:
Hi all,
I have one webpage in which people can order books online.
i have one array - book , in which i have stored all different types of books.
Now, I dont want the user to order any garbage (...
|
by: Faith0G |
last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
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...
|
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
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
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...
| |