User input

S

Simon

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
 
S

santosh

Simon said:
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.
 
M

mark_bluemel

Simon said:
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.
 
F

Fred Kleinschmidt

Simon said:
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
 
S

Simon

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
 
E

Espen Myrland

Simon said:
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 Wolfeschlegelsteinhausenbergerdorffwelchevoralternwarengewissenschaftschaferswessenschafewarenwohlgepflegeundsorgfaltigkeitbeschutzenvonangreifeudurchihrraubgierigfeindewelchevoralternzwolftausendjahresvorandieerscheinenerscheinenvanderersteerdemenschderraumschiffgebrauchlichtalsseinursprungvonkraftgestartseinlangefahrthinzwischensternaitigraumaufdersuchenachdiesternwelchegehabtbewohnbarplanetenkreisedrehensichundwohinderneurassevonverstandigmenschlichkeitkonntefortpflanzenundsicherfeuenanlebenslanglichfreudeundruhemitnicheinfurchtvorangreifenvonandererintelligentgeschopfsvonhinzwischenternart Zeus igraum Senior from using your program? :)
 
M

mark_bluemel

Simon said:
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.
 
S

Simon

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,772
Messages
2,569,593
Members
45,111
Latest member
VetaMcRae
Top