Problem with getch()

This is a discussion on Problem with getch() within the C Programming forums, part of the General Programming Boards category; Guys, I'm a newbie in C programming, and this is one of my first game that I created. This is ...

  1. #1
    Registered User
    Join Date
    Jan 2008
    Posts
    66

    Problem with getch()

    Guys, I'm a newbie in C programming, and this is one of my first game that I created.
    This is just a very simple snake game and so far, I've fixed all problems, except for one.
    Everytime the game begins, you'll have to hit a key to start, but that's where my problem is located. If I hit the up arrow, the game'll start, but the snake will move immediately to the top of the screen, and it's the same if I hit the down arrow. What I want is that after hitting a key, the game starts but the snake should move to the left before the next key is pressed.

    I guessed that this's caused by the remaining entry from getch() in the input stream, so I tried to flush it, but it doesn't help a bit.

    Here's the source code :
    It was compiled on borland C++ 5 and won't work on Dev C++.

    Code:
    #include<stdio.h>
    #include<conio.h>
    #include<stdlib.h>
    #include<time.h>
    #include<string.h>
    #define startx 10
    #define starty 13
    
    struct t_koor {
    int x,y;
    } snake[100];
    
    struct t_score {
    char name[25];
    int score;
    } sscr[10];
    
    int spda=50,bdy=10,i,j;
    int grid[25][80],pos;
    FILE *scr;
    
    void bubblesort(int a)
        {
       int temp,b,c;
       char temp2[25];
       for(i=0;i<9;i++) for(j=9;j>i;j--)
               {
              if(a==1)  {b = j;c = j-1;}
              else if(a==2) {b = j-1;c = j;}
              if(sscr[b].score>sscr[c].score)
                  {
                 temp = sscr[j].score;
                 sscr[j].score = sscr[j-1].score;
                 sscr[j-1].score = temp;
                 strcpy(temp2,sscr[j].name);
                 strcpy(sscr[j].name,sscr[j-1].name);
                 strcpy(sscr[j-1].name,temp2);
                 }
             }
       }
    
    void i_score(int score)
        {
       char name[25];
       clrscr();
       scr = fopen("C:\snkscr.dat","r+");
       for(i=0;i<10;i++) fread(&sscr[i],sizeof(struct t_score),1,scr);
       fclose(scr);
       printf("HIGH SCORE :\n\n");
         for(i=0;i<10;i++) printf("%2d. %s - %d\n",i+1,sscr[i].name,sscr[i].score);
       gotoxy(1,14);printf("Your score is %3d\n",score);getch();
       if(score>=sscr[9].score)
           {
          do{
               gotoxy(1,16);clreol();
              printf("Please input your name! [MAX = 11] ");
              gets(name);
              } while(strlen(name)>11);
           for(i=0;i<10;i++)
               {
              if(score==sscr[9].score) sscr[9].score = score;
              else if(score>=sscr[i+1].score && score<=sscr[i].score)
                  {
                 for(j=9;j>i+1;j--)
                     {
                    sscr[j].score = sscr[j-1].score;
                    strcpy(sscr[j].name,sscr[j-1].name);
                    }
                 strcpy(sscr[i+1].name,name);
                 sscr[i+1].score = score;
                 break;
                 }
              }
           }
       scr = fopen("C:\snkscr.dat","w");
       for(i=0;i<10;i++) fwrite(&sscr[i],sizeof(struct t_score),1,scr);
       fclose(scr);
       printf("\nPress any key to go back to main menu");
       getch();
       }
    
    void s_score(void)
        {
       char key,sure;
       scr = fopen("C:\snkscr.dat","r+");
        for(i=0;i<10;i++) fread(&sscr[i],sizeof(struct t_score),1,scr);
       fclose(scr);
       do {
           clrscr();
           gotoxy(35,2);textcolor(YELLOW);cprintf("HIGH SCORE");
          gotoxy(25,5);textcolor(LIGHTRED);for(i=0;i<30;i++) cprintf("=");
            for(i=0;i<12;i++) {gotoxy(25,i+6);textcolor(LIGHTRED);cprintf("=");}
           for(i=0;i<10;i++) {gotoxy(29,7+i);printf("%2d. %s - %4d\n",i+1,sscr[i].name,sscr[i].score);}
              for(i=0;i<12;i++) {gotoxy(54,i+6);textcolor(LIGHTRED);cprintf("=");}
           gotoxy(25,18);textcolor(LIGHTRED);for(i=0;i<30;i++) cprintf("=");
          gotoxy(1,22);
           printf("1. Reset Highscore\n");
             printf("2. Sort Descending\n");
             printf("3. Sort Ascending\n");
           printf("4. Back to Main Menu\n");
           key = (char) getch();
           if(key=='1')
                  {
                printf("Are you sure ? [Y/N]");
                sure = (char) getch();
                if(sure=='y')
                    {
                    for(i=0;i<10;i++)
                        {
                       strcpy(sscr[i].name,"Snake Mania");
                       sscr[i].score = ((2000/(i+1))/10)*10;
                       }
                    scr = fopen("C:\snkscr.dat","w");
                    for(i=0;i<10;i++) fwrite(&sscr[i],sizeof(struct t_score),1,scr);
                    fclose(scr);
                   }
                 }
          else if(key=='2') bubblesort(1);
          else if(key=='3') bubblesort(2);
          } while(key!='4');
       }
    
    void speed(int sec)
        {
        clock_t timesec;
       timesec = clock();
       while((clock() - timesec) < sec);
        }
    
    void move(char arah,int body)
        {
       switch(arah)
           {
          case 72:
              {
             snake[body].x = snake[body-1].x;
             snake[body].y = snake[body-1].y - 1;
             }
          break;
          case 75:
              {
              snake[body].x = snake[body-1].x - 1;
              snake[body].y = snake[body-1].y;
              }
          break;
          case 77:
              {
              snake[body].x = snake[body-1].x + 1;
              snake[body].y = snake[body-1].y;
              }
          break;
          case 80:
              {
              snake[body].x = snake[body-1].x;
              snake[body].y = snake[body-1].y + 1;
              }
          }
       }
    
    void lvlgrid(int lvl)
        {
        if(lvl==1)
           {
          for(i=0;i<80;i++) grid[0][i] = 1;
           for(i=0;i<80;i++) grid[23][i] = 1;
           for(i=1;i<24;i++) grid[i][0] = 1;
             for(i=1;i<24;i++) grid[i][79] = 1;
          }
       else if(lvl==2)
           {
           for(i=19;i<60;i++) grid[6][i] = 1;
           for(i=19;i<60;i++) grid[16][i] = 1;
          }
       else  if(lvl==3)
           {
           for(i=5;i<21;i++) grid[i][5] = 1;
           for(i=5;i<21;i++) grid[i][75] = 1;
          }
       else if(lvl==4)
           {
           for(i=10;i<13;i++) grid[i][19] = 1;
           for(i=10;i<13;i++) grid[i][59] = 1;
          }
       else if(lvl==5)
           {
          for(i=24;i<55;i++) grid[4][i] = 1;
           for(i=24;i<55;i++) grid[19][i] = 1;
          }
       else if(lvl==6)
           {
          for(i=0;i<25;i++) for(j=0;j<80;j++) grid[i][j] = 0;
            for(i=0;i<80;i++) {grid[0][i] = 1;grid[23][i] = 1;}
             for(i=0;i<30;i++) {grid[5][i] = 1;grid[5][i+50] = 1;grid[18][i] = 1;grid[18][i+50] = 1;}
          for(i=1;i<5;i++) {grid[i][0] = 1;grid[i+18][0] = 1;grid[i][79] = 1;grid[i+18][79] = 1;}
          for(i=0;i<14;i++) {grid[i+5][30] = 1;grid[i+5][49] = 1;}
          for(i=0;i<11;i++) grid[i+7][39] = 1;
          grid[11][30] =  0;grid[12][30] =  0;
          grid[11][49] =  0;grid[12][49] =  0;
          }
       textcolor(LIGHTBLUE);
           for(i=0;i<25;i++) for(j=0;j<80;j++)
           {
          if(grid[i][j]==1)
              {
             gotoxy(j+1,i+1);
             cprintf("=");
             }
          }
       }
    
    void ular(int spd,int body)
        {
       int foodx,foody,chc;
       int bonusx,bonusy;
       int xtx,xty,xttm,xtchc;
       int score=0,lvl=1;
       int t1=1,t2=1,b1=1,b2=1;
       char key,arah=77,game,new = 1;
       clrscr();
       pos = 1;
       chc = random(2);
       xttm = random(5);
       for(i=0;i<25;i++) for(j=0;j<80;j++) grid[i][j] = 0;
       lvlgrid(1);
       for(i=0;i<body;i++)
           {
          snake[i].y = starty;
          snake[i].x = startx + i;
          }
       do {
           foodx = random(78) + 2;
           foody = random(22) + 2;
           bonusx = random(78) + 2;
           bonusy = random(22) + 2;
          for(i=0;i<body;i++) if((snake[i].x==foodx && snake[i].y==foody) || (snake[i].x==bonusx && snake[i].y==bonusy)) break;
          } while((bonusx==foodx && bonusy==foody) || grid[bonusy-1][bonusx-1]==1 || grid[foody-1][foodx-1]==1 || i!=body);
       gotoxy(1,25);
       textcolor(LIGHTRED);
       cprintf("SCORE :");
       gotoxy(14,25);
       cprintf("BODY LENGTH :");
       gotoxy(33,25);
       cprintf("LEVEL :");
       do{
          gotoxy(9,25);
           textcolor(YELLOW);
          cprintf("%3d  ",score);
          gotoxy(27,25);
            cprintf("%3d  ",body);
          gotoxy(41,25);
            cprintf("%d",lvl);
          gotoxy(50,25);clreol();
          textcolor(LIGHTRED);
          cprintf("<P> = PAUSE THE GAME!");
          if(pos!=lvl)
              {
             pos = lvl;
             gotoxy(50,25);clreol();
             textcolor(LIGHTRED);
             cprintf("LEVEL UP!");
             speed(2000);
             gotoxy(60,25);clreol();
             for(i=0;i<body;i++)
                  {
                 gotoxy(snake[i].x,snake[i].y);
                 printf(" ");
                 }
             gotoxy(t1,t2);printf(" ");
             gotoxy(b1,b2);printf(" ");
             body = bdy;
             arah = 77;
             new = 1;
             for(i=0;i<body;i++)
                   {
                  snake[i].y = starty;
                  snake[i].x = startx + i;
                  }
             lvlgrid(lvl);
             do {
                 foodx = random(78) + 2;
                foody = random(22) + 2;
                for(i=0;i<body;i++) if(snake[i].x==foodx && snake[i].y==foody) break;
                } while((bonusx==foodx && bonusy==foody) || grid[foody-1][foodx-1]==1 || i!=body);
             }
          if(lvl>=3 && xttm==0)
              {
             xtchc = random(1271);
             if(xtchc==random(774)-body+53)
                 {
                do {
                       xtx = random(78) + 2;
                       xty = random(22) + 2;
                      for(i=0;i<body;i++) if(snake[i].x==xtx && snake[i].y==xty) break;
                      } while((xtx==foodx && xty==foody) || (xtx==bonusx && xty==bonusy) || grid[xty-1][xtx-1]==1 || i!=body);
                xttm = random(25) + 26;
                }
                }
          if(xttm!=0)
              {
             gotoxy(xtx,xty);
              textcolor(YELLOW);
              cprintf("");
             xttm--;
             }
          if(xttm==0) {gotoxy(xtx,xty);printf(" ");}
           if(chc==1)
               {
              gotoxy(bonusx,bonusy);
              textcolor(LIGHTRED);
              cprintf("$");
              }
          gotoxy(foodx,foody);textcolor(LIGHTGREEN);cprintf("@");
          for(i=0;i<body;i++)
              {
             gotoxy(snake[i].x,snake[i].y);
             textcolor(YELLOW);cprintf("*");
             }
          if(kbhit())
               { 
              key = (char) getch();
             if(key=='p')
                 {
                gotoxy(50,25);clreol();
                textcolor(LIGHTRED);
                cprintf("PRESS ANY KEY TO RESUME GAME!");
                getch();
                }
                else if((key==72 && arah!=80) || (key==75 && arah!=77) || (key==77 && arah!=75) || (key==80 && arah!=72)) arah = key;
              speed(30);
              if(kbhit())
                  {       
                 key = (char) getch();
                 if((key==72 && arah!=80) || (key==75 && arah!=77) || (key==77 && arah!=75) || (key==80 && arah!=72)) arah = key;
                 } 
                  }
          if(new==1)
              {
             gotoxy(50,25);clreol();
             textcolor(LIGHTRED);
             cprintf("PRESS ANY KEY TO START!");
             getch();
             gotoxy(50,25);clreol();
             new = 0;
             }
          move(arah,body);
          speed(spd);
          gotoxy(snake[0].x,snake[0].y);printf(" ");
          for(i=0;i<body;i++) snake[i] = snake[i+1];
          if(snake[body-1].x==81) snake[body-1].x = 1;
          else if(snake[body-1].x==0) snake[body-1].x = 80;
          for(i=1;i<body;i++) if((snake[body-1].x)==(snake[i-1].x) && (snake[body-1].y)==(snake[i-1].y))
              {
             game='x';
             gotoxy(50,25);clreol();
             textcolor(LIGHTRED);
             cprintf("YOU LOSE!");
             getch();
             break;
             }
          if(grid[snake[body-1].y-1][snake[body-1].x-1]==1)
              {
             gotoxy(50,25);clreol();
             textcolor(LIGHTRED);
             cprintf("YOU LOSE!");
             game = 'x';
             getch();
             }
          b1 = bonusx;b2 = bonusy;
          t1 = foodx;t2 = foody;
          if(snake[body-1].x==foodx && snake[body-1].y==foody)
              {
                do {
                 foodx = random(78) + 2;
                foody = random(22) + 2;
                for(i=0;i<body;i++) if(snake[i].x==foodx && snake[i].y==foody) break;
                } while((bonusx==foodx && bonusy==foody) || grid[foody-1][foodx-1]==1 || i!=body);
             score+=10;
             body++;
             }
          if(snake[body-1].x==bonusx && snake[body-1].y==bonusy)
              {
             do {
                 bonusx = random(78) + 2;
                bonusy = random(22) + 2;
                for(i=0;i<body;i++) if(snake[i].x==bonusx && snake[i].y==bonusy) break;
                } while((bonusx==foodx && bonusy==foody) || grid[bonusy-1][bonusx-1]==1 || i!=body);
             score+=50;
             chc = random(2);
             }
          if(snake[body-1].x==xtx && snake[body-1].y==xty)
              {
             do {
                 xtx = random(78) + 2;
                xty = random(22) + 2;
                for(i=0;i<body;i++) if(snake[i].x==xtx && snake[i].y==xty) break;
                } while((xtx==foodx && xty==foody) || (xtx==bonusx && xty==bonusy) || grid[xty-1][xtx-1]==1 || i!=body);
             score+=200;
             xttm = 0;
             }
          if(score>=250 && score<500) lvl = 2;
          else if(score>=500 && score<850) lvl = 3;
           else if(score>=850 && score<1200) lvl = 4;
          else if(score>=1200) lvl = 5;
          else if(score>=1500) lvl = 6;
          } while(game!='x' && key!=27);
       i_score(score);
       }
      
    void diff(void)
        {
       char sel;
       do {
          gotoxy(1,12);
          printf("================\n");
           printf("1. Very Easy  ||\n");
           printf("2. Easy       ||\n");
           printf("3. Normal     ||\n");
           printf("4. Medium     ||\n");
          printf("5. Hard       ||\n");
             printf("6. Very Hard  ||\n");
             printf("================\n");
           sel = (char) getch();
           switch(sel)
               {
              case '1': spda = 100;bdy = 5;
             break;
              case '2': spda = 75;bdy = 8;
             break;
             case '3': spda = 40;bdy = 10;
             break;
             case '4': spda = 20;bdy = 13;
             break;
             case '5': spda = 15;bdy = 17;
             break;
             case '6': spda = 5;bdy = 20;
              }
          } while(sel!='1' && sel!='2' && sel!='3' && sel!='4' && sel!='5' && sel!='6');
       gotoxy(1,12);clreol();gotoxy(1,13);clreol();gotoxy(1,14);clreol();
       gotoxy(1,15);clreol();gotoxy(1,16);clreol();gotoxy(1,17);clreol();
       gotoxy(1,18);clreol();gotoxy(1,19);clreol();gotoxy(1,20);clreol();
       }
      
    void title(void)
        {
       gotoxy(21,2);textcolor(LIGHTBLUE);cprintf("======================================");
       gotoxy(21,3);textcolor(YELLOW);cprintf("****   **   *      *      *   *  *****");
       gotoxy(21,4);cprintf("*      * *  *     * *     *  *   *");
       gotoxy(21,5);cprintf("****   *  * *    *   *    * *    *****");
       gotoxy(21,6);cprintf("   *   *   **   *******   *  *   *");
       gotoxy(21,7);cprintf("****   *    *  *       *  *   *  *****");
       gotoxy(21,8);textcolor(LIGHTRED);cprintf("======================================");
       gotoxy(1,24);for(i=0;i<80;i++) printf("_");
       }
    
    void menu(void)
        {
       int exit=0,temp;
       int SATU=15,DUA=0,TIGA=0,EMPAT=0,TEXT=15;
       char key;
       title();
       do {
          textcolor(TEXT);
          gotoxy(36,11);textbackground(SATU);cprintf("NEW GAME");
           gotoxy(35,13);textbackground(DUA);cprintf("DIFFICULTY");
          gotoxy(35,15);textbackground(TIGA);cprintf("HIGH SCORE");
          gotoxy(38,17);textbackground(EMPAT);cprintf("EXIT");
          textbackground(BLACK);
          key = (char) getch();
          switch(key)
              {
             case 72:
                 {
                temp = SATU;                 
                SATU = DUA;
                DUA = TIGA;
                TIGA = EMPAT;
                EMPAT = temp;
                }
             break;
             case 80:
                 {
                temp = EMPAT;
                EMPAT = TIGA;
                TIGA = DUA;
                DUA = SATU;
                SATU = temp;
                }
             break;
             case 13:
                 {
                if(SATU==15) {ular(spda,bdy);clrscr();title();}
                else if(DUA==15) diff();
                else if(TIGA==15) {s_score();clrscr();title();}
                else if(EMPAT==15) exit = 1;
                }
             }
           } while(!exit);
       }
    
    int main()
        {
       _setcursortype(_NOCURSOR);
       randomize();
       if((scr = fopen("C:\snkscr.dat","r"))==NULL)
           {
          scr = fopen("C:\snkscr.dat","w");
          for(i=0;i<10;i++)
              {
             strcpy(sscr[i].name,"Snake Mania");
             sscr[i].score = ((2000/(i+1))/10)*10;
             }
          for(i=0;i<10;i++) fwrite(&sscr[i],sizeof(struct t_score),1,scr);
          }
       fclose(scr);
       menu();
       return 0;
       }

  2. #2
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    First, please learn to indent properly, it will help you and others that read your code immensely.
    Next, you do know that getch() and the <conio.h> header are not part of the C standard right? They are only available on certain platforms/compilers.

    Check the FAQ on getting single key presses. Here are some I found about flushing the input buffer:
    Why fflush(stdin) is wrong
    Flush the input buffer

  3. #3
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,246
    Quote Originally Posted by cpjust View Post
    First, please learn to indent properly, it will help you and others that read your code immensely.
    The indentation is fine. Not the most common style, but consistent and logical.

  4. #4
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,667
    The indentation is certainly not poor, but not perfect either. Plus it's erratic and inconsistent.
    I would love to suggest better indentation too, though the code is readable, but the better, the more it helps.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User
    Join Date
    Jan 2008
    Posts
    66
    Thanks for the suggestion!
    The indentation got messed up, because it was copy pasted from notepad.
    Well then, let's just go to the point!

    Code:
    do{
    
    [Rest of The Code]
           if(new==1)
              {
             gotoxy(50,25);clreol();
             textcolor(LIGHTRED);
             cprintf("PRESS ANY KEY TO START!");
             getch();
             gotoxy(50,25);
             clreol();
             new = 0;
             }      
    
            if(kbhit())
                  {       
                  key = (char) getch();
                  } 
    
    [Rest of the code]
    
    } while(game!='x' && key!=27);
    I combined kbhit() and getch() to make key pressing possible and there lies the problem.
    When new = 1, when the game begin, that is, you'll have to hit a key.
    When I hit a key, like the up arrow, the key pressed that time will remain in the input stream and getch() will accept it as as an input. So the question is, how do I remove that key from the input stream? fflush(stdin) doesn't help...
    I tried to change getch() below cprintf("PRESS ANY KEY TO START!"); to getchar(), but it doesn't help either.
    Please help me fix this problem guys! Thank you!

    And one more thing...
    If getch() isn't good to use since it's not a part of the C standard, what should I use to replace it?
    Last edited by ThLstN; 01-05-2008 at 11:27 PM.

  6. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,590
    > When I hit a key, like the up arrow, the key pressed that time will remain in the input stream and getch()
    You do realise that things like up-arrow are actually sent as TWO bytes (meaning you have to call getch twice).
    There are a couple of 'magic' first bytes which tell you another byte is on it's way.

    If you don't read them both, you're going to get out of step.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  7. #7
    Registered User
    Join Date
    Jan 2008
    Posts
    66
    Well, I am a newbie, I would never realize that an up arrow's size is 2 bytes
    So I need to call getch() twice in order to remove the remaining byte in the input stream
    I tried and it won't work either, what should I do?
    What about putting getch() in a variable? Will that remove the input stream?
    But if I hit a key that has a size of 1 byte, then I have to hit the key twice right?

  8. #8
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,590
    The second byte is the real information, the first byte is a constant.

    I typically would do
    Code:
    ch = getch();
    if ( ch == 0xE0 ) ch = 256 + getch();
    Oh, and 0xE0 is only a guess (or a vague memory), you'll need to actually look up the value yourself (or test to find out).

    So all normal keys are in the range 0..255
    And all extended keys are in the range 256..511.

    You'll need to change all your case constants which relate to arrow keys if you do this.

    > I would never realize that an up arrow's size is 2 bytes
    Write small test programs to test ideas, especially when things start behaving "oddly".
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  9. #9
    Registered User
    Join Date
    Jan 2008
    Posts
    66
    Quote Originally Posted by Salem View Post
    The second byte is the real information, the first byte is a constant.

    Oh, and 0xE0 is only a guess (or a vague memory), you'll need to actually look up the value yourself (or test to find out).
    How can I get the memory information?
    Is that by printing the value of ch in hexadesimal?

  10. #10
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,590
    You can print it in hex if you want.
    Or you can print it in decimal, and then write
    if ( ch == 224 )
    or whatever the value is, when you find it.

    The base doesn't make any difference.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  11. #11
    Registered User
    Join Date
    Jan 2008
    Posts
    66
    Code:
    ch = getch();
    printf("%x",ch);
    I suppose this is not the correct way to do it, because I got the value of the key itself.
    Please tell me how to do it correctly, I'm a real newbie and have been learning C for only about 4 months. Thanks for your help!

  12. #12
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by Salem View Post
    Oh, and 0xE0 is only a guess (or a vague memory), you'll need to actually look up the value yourself (or test to find out).

    So all normal keys are in the range 0..255
    And all extended keys are in the range 256..511.

    You'll need to change all your case constants which relate to arrow keys if you do this.

    > I would never realize that an up arrow's size is 2 bytes
    Write small test programs to test ideas, especially when things start behaving "oddly".
    Wow, that's a pretty good guess!
    Quote Originally Posted by MSDN
    The _getch function reads a single character from the console without echoing. _getche reads a single character from the console and echoes the character read. Neither function can be used to read CTRL+C. When reading a function key or an arrow key, _getch and _getche must be called twice; the first call returns 0 or 0xE0, and the second call returns the actual key code.
    Code:
    key = (char) getch();
    BTW, getch() returns an int, not a char. If the extended keys are above 255 as Salem says, then casting to a char will truncate that number.

  13. #13
    Registered User
    Join Date
    Jan 2008
    Posts
    66
    Thanks guys! Thanks to you I understand how getch() actually works and now it's fixed!

    Just by doing this:

    Code:
      cprintf("PRESS ANY KEY TO START!");
      trash = getch();
      if(trash==0) getch();

  14. #14
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,667
    The point was that if getch returned 0 OR 0xE0, you must call getch again to get the actual key code.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Words and lines count problem
    By emo in forum C Programming
    Replies: 1
    Last Post: 07-12-2005, 03:36 PM
  2. Problem with my file opener
    By kzar in forum C Programming
    Replies: 7
    Last Post: 04-20-2005, 04:20 PM
  3. half ADT (nested struct) problem...
    By CyC|OpS in forum C Programming
    Replies: 1
    Last Post: 10-26-2002, 08:37 AM
  4. binary tree problem - help needed
    By sanju in forum C Programming
    Replies: 4
    Last Post: 10-16-2002, 05:18 AM
  5. Problem with getch() and fgetc()
    By Krupux in forum C Programming
    Replies: 3
    Last Post: 10-01-2002, 11:38 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21