473,383 Members | 1,877 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,383 software developers and data experts.

Allowing 1 input only

2
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);
Mar 3 '10 #1
10 2947
jkmyoung
2,057 Expert 2GB
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?
Mar 3 '10 #2
You should use cscanf to allow only 1 input.

cscanf("%c",&char);
Mar 3 '10 #3
harz
2
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?
Mar 3 '10 #4
I think there is no other way.
Mar 3 '10 #5
weaknessforcats
9,208 Expert Mod 8TB
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.
Mar 3 '10 #6
donbock
2,426 Expert 2GB
You should get in the habit of using this sequence to prompt for keyboard input:
Expand|Select|Wrap|Line Numbers
  1. flush stdin
  2. write prompt string to stdout
  3. 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.
Mar 3 '10 #7
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().
Mar 3 '10 #8
donbock
2,426 Expert 2GB
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?
Mar 4 '10 #9
johny10151981
1,059 1GB
what about getch and getchar function. have you tried?

My bad Banfa already answered it.
Mar 4 '10 #10
weaknessforcats
9,208 Expert Mod 8TB
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.
Mar 4 '10 #11

Sign in to post your reply or Sign up for a free account.

Similar topics

3
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...
5
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...
3
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...
2
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...
5
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...
13
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...
5
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...
2
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...
3
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"...
15
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 (...
0
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...
0
isladogs
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...
0
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,...
0
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...
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?
1
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...
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...

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.