Thread: need help with getch

  1. #1
    adrive
    Guest

    Unhappy 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;

    }

  2. #2
    adrive
    Guest
    erm....
    is there anyone who're willing to help??? : ((

  3. #3
    Registered User
    Join Date
    Feb 2002
    Posts
    25
    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--;
    }

    }
    inZane
    --true programmer's don't comment--
    --programmer wannabes complain about it--

  4. #4
    adrive
    Guest
    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--;
    }

    }



    }

  5. #5
    Registered User
    Join Date
    Aug 2001
    Posts
    129
    Code:
    if((ch == '\b') && (i > 0))
    {
        printf("\b \b");
        i--;
        sEntry[i] = '\0'; // Need this one?
    }
    ps.
    No need for that kbhit()...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  2. Pls repair my basketball program
    By death_messiah12 in forum C++ Programming
    Replies: 10
    Last Post: 12-11-2006, 05:15 AM
  3. Pause a C program without getch()
    By swgh in forum C Programming
    Replies: 4
    Last Post: 02-20-2006, 11:24 AM
  4. Clearing input buffer after using getch()
    By milkydoo in forum C++ Programming
    Replies: 3
    Last Post: 07-21-2003, 11:04 PM
  5. Problems with getch()
    By GrNxxDaY in forum C++ Programming
    Replies: 14
    Last Post: 08-12-2002, 02:11 AM