www.digitalmars.com         C & C++   DMDScript  

c++ - How can I restrict the cursor not to go further...?

reply Alexandar <Alexandar_member pathlink.com> writes:
How can I restrict the cursor not to go further...i.e. when I carry out the
program and the program waits for numbers or chars isertion, I want to allow
just a determined number (for instance 4) of digits or chars to be entered. It
won't allow me to enter the fifth char and will wait for ENTER.

Thank you in advance, Alexandar
Mar 17 2006
parent reply Bertel Brander <bertel post4.tele.dk> writes:
Alexandar wrote:
 How can I restrict the cursor not to go further...i.e. when I carry out the
 program and the program waits for numbers or chars isertion, I want to allow
 just a determined number (for instance 4) of digits or chars to be entered. It
 won't allow me to enter the fifth char and will wait for ENTER.
#include <stdio.h> #include <conio.h> int main() { char Text[5]; int i; for(i = 0; i < 4; i++) { Text[i] = getch(); putch(Text[i]); } Text[i] = 0; while(getch() != 13) ; printf("\nYou entered: %s\n", Text); } -- Absolutely not the best homepage on the net: http://home20.inet.tele.dk/midgaard But it's mine - Bertel
Mar 17 2006
parent Bertel Brander <bertel post4.tele.dk> writes:
Bertel Brander wrote:
 Alexandar wrote:
 How can I restrict the cursor not to go further...i.e. when I carry 
 out the
 program and the program waits for numbers or chars isertion, I want to 
 allow
 just a determined number (for instance 4) of digits or chars to be 
 entered. It
 won't allow me to enter the fifth char and will wait for ENTER.
As a side note; one might think that this should do the trick: #include <stdio.h> #include <conio.h> int main() { char Text[5]; int i; for(i = 0; i < 4; i++) { Text[i] = getche(); } Text[i] = 0; while(getch() != 13) ; printf("\nYou entered: %s\n", Text); } But it does not, all the characteres entered are echoed on screen, but only 4 put into Text, as if both getch's was getche. -- Absolutely not the best homepage on the net: http://home20.inet.tele.dk/midgaard But it's mine - Bertel
Mar 17 2006