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

User input

I'm trying to find some nice, concise code that asks the user for their
name and then stores it. I'd like it to behave as follows:

1. The user can type in up to 12 letters or numbers
2. Once 12 letters or numbers have been entered onto the screen,
nothing is displayed if they press further keys
3. If there are 12 letters on the screen and they user has made a
mistake then they can delete letters using backspace
4. The code only saves the user's name once enter is pressed (ie check
for '\n')

Theoretically this should be very easy but I've been having an
incredible amount of difficulty with it. I'm sure that it's been done
thousands of times before but I can't find any examples. Can anyone
help?

Thanks in advance

Simon

Nov 29 '06 #1
7 1593
Simon wrote:
I'm trying to find some nice, concise code that asks the user for their
name and then stores it. I'd like it to behave as follows:

1. The user can type in up to 12 letters or numbers
2. Once 12 letters or numbers have been entered onto the screen,
nothing is displayed if they press further keys
3. If there are 12 letters on the screen and they user has made a
mistake then they can delete letters using backspace
4. The code only saves the user's name once enter is pressed (ie check
for '\n')

Theoretically this should be very easy but I've been having an
incredible amount of difficulty with it. I'm sure that it's been done
thousands of times before but I can't find any examples. Can anyone
help?

Thanks in advance
Number two and three can't be done within standard C. You need low
level console access via either extensions to your C library or a third
party library like curses, or directly call the screen control routines
of your OS.

Once you get the low level routines done, (for which post in a more
platform relevant group), number one and four can be easily
accomplished by standard C.

Nov 29 '06 #2

Simon wrote:
I'm trying to find some nice, concise code that asks the user for their
name and then stores it. I'd like it to behave as follows:

1. The user can type in up to 12 letters or numbers
2. Once 12 letters or numbers have been entered onto the screen,
nothing is displayed if they press further keys
3. If there are 12 letters on the screen and they user has made a
mistake then they can delete letters using backspace
4. The code only saves the user's name once enter is pressed (ie check
for '\n')

Theoretically this should be very easy but I've been having an
incredible amount of difficulty with it. I'm sure that it's been done
thousands of times before but I can't find any examples. Can anyone
help?
This is not very easy at all, at least not if you use the standard I/O
facilities of the C language.

You are going to have to
* read input a character at a time unbuffered (normally input will
be buffered
and take place a line at a time),

* do your own echoing (which means you have to disable the default
terminal echoing,

* handle a wide range of "unusual input" (backspace or del? Do you
want to handle
arrow keys as well?)

* and more...

You probably need to look at non-standard but common solutions, which
will almost certainly be platform-specific. On Un*x-like systems,
"curses" may be appropriate, on Windoze, I don't know.

You may do well to ask on a newsgroup related to your platform.

Nov 29 '06 #3

ma**********@pobox.com wrote:
This is not very easy at all, at least not if you use the standard I/O
facilities of the C language.

You are going to have to
* read input a character at a time unbuffered (normally input will
be buffered
and take place a line at a time),

* do your own echoing (which means you have to disable the default
terminal echoing,

* handle a wide range of "unusual input" (backspace or del? Do you
want to handle
arrow keys as well?)

* and more...

You probably need to look at non-standard but common solutions, which
will almost certainly be platform-specific. On Un*x-like systems,
"curses" may be appropriate, on Windoze, I don't know.

You may do well to ask on a newsgroup related to your platform.
Ok - I thought there may be a more obvious way to do it but I've got
some experience of pdcurses (using Windows and MingGW by the way). I
guess it goes something like this:

1. Turn echo off
2. When a key is pressed, print it on the screen
3. Keep track of number of letters on screen (i)
4. If i = 12, stop printing letters on screen when they're pressed
5. If backspace is pressed, reduce i by 1
6. When return is pressed, shove the whole lot into a char array to use
later

This is a bit fiddly (you need to make sure you cover the special case
i = 0 (backspace does nothing) as well as i = 12, but it shouldn't be
insurmountable. Cursor positioning is a bit of a pain in the general
case but presumably I can use strlen() to find the initial position at
the end of the question and then simply increment the x coordinate.

Simon

Nov 29 '06 #4
"Simon" <ya*********@googlemail.comwrites:
I'm trying to find some nice, concise code that asks the user for their
name and then stores it. I'd like it to behave as follows:

1. The user can type in up to 12 letters or numbers
2. Once 12 letters or numbers have been entered onto the screen,
nothing is displayed if they press further keys
3. If there are 12 letters on the screen and they user has made a
mistake then they can delete letters using backspace
4. The code only saves the user's name once enter is pressed (ie check
for '\n')

Theoretically this should be very easy but I've been having an
incredible amount of difficulty with it. I'm sure that it's been done
thousands of times before but I can't find any examples. Can anyone
help?

Why do you want to prevent Mr. Adolph Blaine Charles David Earl Frederick
Gerald Hubert Irvin John Kenneth Lloyd Martin Nero Oliver Paul Quincy
Randolph Sherman Thomas Uncas Victor William Xerxes Yancy Wolfeschlegelsteinhausenbergerdorffwelchevoraltern warengewissenschaftschaferswessenschafewarenwohlge pflegeundsorgfaltigkeitbeschutzenvonangreifeudurch ihrraubgierigfeindewelchevoralternzwolftausendjahr esvorandieerscheinenerscheinenvanderersteerdemensc hderraumschiffgebrauchlichtalsseinursprungvonkraft gestartseinlangefahrthinzwischensternaitigraumaufd ersuchenachdiesternwelchegehabtbewohnbarplanetenkr eisedrehensichundwohinderneurassevonverstandigmens chlichkeitkonntefortpflanzenundsicherfeuenanlebens langlichfreudeundruhemitnicheinfurchtvorangreifenv onandererintelligentgeschopfsvonhinzwischenternart Zeus igraum Senior from using your program? :-)
--
/myr
Nov 29 '06 #5

Simon wrote:
>
Ok - I thought there may be a more obvious way to do it but I've got
some experience of pdcurses (using Windows and MingGW by the way). I
guess it goes something like this:

1. Turn echo off
2. When a key is pressed, print it on the screen
We are heading off-topic for clc (this is general programming, not
really specific to C), but don't forget that terminal input has usually
been heavily processed by the time you see it, so you may need to think
about which keys you echo, which you handle specially (ENTER, RETURN,
Backspace, Del, what about "Ins" ....), and which you ignore.

Nov 30 '06 #6

ma**********@pobox.com wrote:
Simon wrote:

Ok - I thought there may be a more obvious way to do it but I've got
some experience of pdcurses (using Windows and MingGW by the way). I
guess it goes something like this:

1. Turn echo off
2. When a key is pressed, print it on the screen

We are heading off-topic for clc (this is general programming, not
really specific to C), but don't forget that terminal input has usually
been heavily processed by the time you see it, so you may need to think
about which keys you echo, which you handle specially (ENTER, RETURN,
Backspace, Del, what about "Ins" ....), and which you ignore.
Agreed - the special cases make this a bit of a painful task...

Simon

Nov 30 '06 #7

"Simon" <ya*********@googlemail.comwrote in message
news:11**********************@l12g2000cwl.googlegr oups.com...
I'm trying to find some nice, concise code that asks the user for their
name and then stores it. I'd like it to behave as follows:

1. The user can type in up to 12 letters or numbers
2. Once 12 letters or numbers have been entered onto the screen,
nothing is displayed if they press further keys
3. If there are 12 letters on the screen and they user has made a
mistake then they can delete letters using backspace
4. The code only saves the user's name once enter is pressed (ie check
for '\n')

Theoretically this should be very easy but I've been having an
incredible amount of difficulty with it. I'm sure that it's been done
thousands of times before but I can't find any examples. Can anyone
help?

Thanks in advance

Simon
Look at JFormattedTextField
--
Fred L. Kleinschmidt
Boeing Associate Technical Fellow
Technical Architect, Software Reuse Project
Dec 1 '06 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: N?ant Humain | last post by:
I have just begun learning Python so that I can write a simple script to make modification of a file used by another Python script easier. This file is basically a list of regular expressions. What...
10
by: jeff regoord | last post by:
A user inputs a float value. The scanf() function gets the value. However, I need to create an error handler with an if else statement saying invalid input if the input is not a number. Does...
4
by: santa19992000 | last post by:
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,...
3
by: dei3cmix | last post by:
Hey, I am having a problem with a program I am working on. Basically, the first part of the program gets input from a file using cin.getline. Then the second part, (still in the same main as the...
9
by: MadingS | last post by:
Is there an HTML (or CSS) way to say "use the user's INPUT font, whatever that might be."? Most browsers have the ability for the user to pick his or her own preference for what font to use...
2
by: underground | last post by:
Hi, everyone I've been trying to figure out a way for a user to update there information. I'm using sections to identify the specific user..Here is the form <? include("include/session.php");...
5
by: no1zson | last post by:
I have been reading through many of the array questions and cannot find one that addresses my issue. Maybe someone can help me out. Same story, I am learning Java and have just written a CD...
1
by: kang jia | last post by:
hi when user entered particulars in signuppage and click" signup" button, i will direct them to do_signup.php. if say the NRIC is dupicate in datebase, i will redirect them back to signup page...
82
by: happyse27 | last post by:
Hi All, I modified the user registration script, but not sure how to make it check for each variable in terms of preventing junk registration and invalid characters? Two codes below : a)...
9
by: happyse27 | last post by:
Hi All, In perl script(item b below) where we check if html registration form are filled in properly without blank with the necessary fields, how to prompt users that the field are incomplete...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

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.