-
need help with getch
hi...i need help with this segment of program....i wanna let user key in (not more than 3 characters) and allows him to backspace
for(i=0;i<3;i++)
{
choice2[i]=getch();
if(choice2[i]!='\b')
{
gotoxy(43+i,17);
putch(choice2[i]);
}
if(choice2[i]=='\b')
{
putch(' ');
gotoxy(43+i-1,17);
i--;
}
if(choice2[i]=='\r')
break;
}
-
erm....
is there anyone who're willing to help??? : ((
-
you might want to look into using kbhit()
I have not tested or intend to test the code below but I have done something
#include<conio.h>
char sEntry[4] = ""
i = 0;
while( i < 3 )
{
//endless loop
while( !kbhit() );
char ch = getch();
if( ch != '\b' )
{
putchar( ch );
sEntry[i++] = ch;
}
else
{
putchar('\b');
putchar(' ');
putchar('\b');
i--;
}
}
-
ok...i tried modifying it to the code below but notice if i press backspace more than 3 times, the characters needed to enter would increase (seems to be a problem):
#include<conio.h>
char sEntry[4] = "" ;
int i = 0;
void main()
{
while( i < 3 )
{
//endless loop
while( !kbhit() );
char ch = getch();
if( ch != '\b' )
{
putch( ch );
sEntry[i++] = ch;
}
else
{
putch('\b');
putch(' ');
putch('\b');
i--;
}
}
}
-
Code:
if((ch == '\b') && (i > 0))
{
printf("\b \b");
i--;
sEntry[i] = '\0'; // Need this one?
}
ps.
No need for that kbhit()...