title: My C++ program in Turbo C++ gets hanged after some time
body: <p>It is a rather simple snake game. It just gets hanged.
  When I try to execute it, it runs for a while and then hangs in some moments.
  Please Tell me what to do?
  This is turbo C++ version 3.0 and please do not tell me to use new version. And don't call me outdated, I know that.</p>
  
  <pre><code>#include&lt;conio.h&gt;
  #include&lt;iostream.h&gt;
  #include&lt;dos.h&gt;
  #include&lt;graphics.h&gt;
  #include&lt;stdio.h&gt;
  #include&lt;stdlib.h&gt;
  #include&lt;math.h&gt;
  int x,y,a,b,flag,i,discx,discy,sc,maxx,maxy,same;
  short int arr[100][2];
  char ch,check;
  void shift();
  int introscreen();
  void condition();
  void gameover();
  void score();
  void randomapple();
  
  int introscreen()
      {
       outtextxy(maxx/2,30,"SNAKE");
       outtextxy((maxx/2)-10,50,"INSTRUCTIONS");
       outtextxy(5,70,"W A S D represent Top Right Bottom Left respectively.");
       outtextxy(5,90,"Press Spacebar to quit any moment");
       outtextxy(5,110,"To play Press Space else anything else");
       char take=getch();
       if(take==' ')
       return 1;
       else
       return 0;
       }
  void randomapple()
  {same=0;
  while(1)
  {
   a=random(maxx-5);
   b=random(maxy-5);
   for(i=0;i&lt;flag;i++)
   {if((abs(a-arr[i][0]&lt;=5)&amp;&amp;abs(b-arr[i][1]&lt;=5))||a&lt;=5||b&lt;=5)
    same=1;
   }
   if(same==0) break;
   }
   setcolor(RED);
   outtextxy(a,b,"0");
   setcolor(WHITE);
  }
  void shift()
      {discx=arr[flag-1][0];
       discy=arr[flag-1][1];
       for(i=flag-1;i&gt;0;i--)
          {
           arr[i][0]=arr[i-1][0];
           arr[i][1]=arr[i-1][1];
           }
       arr[0][0]=x;
       arr[0][1]=y;
       }
  void printsnake()
      {
       setcolor(BLACK);
       outtextxy(discx,discy,"X");
       setcolor(GREEN);
       outtextxy(x,y,"X");
       setcolor(RED);
       outtextxy(a,b,"0");
       rectangle(0,0,maxx,maxy);
       delay(5);
      }
  void condition()
      {
       switch(ch)
          {
           case'd':x++;
               break;
           case'a':x--;
               break;
           case'w':y--;
               break;
           case's':y++;
               break;
           case' ':exit(1);
  
          }
       }
      void score()
      {if((abs(x-a)&lt;=5)&amp;&amp;(abs(y-b)&lt;=5))
       {sc++;
       if(flag&lt;500)
       flag=flag+10;
       setcolor(BLACK);
       outtextxy(a,b,"0");
       randomapple();
       }
  
      if(sc==100)
      {
          clrscr();
          cout&lt;&lt;"You are the champion tonight!!!!";
          getch();
          exit(1);
      }
      }
  void collision()
      {
       if(x&lt;=5||y&lt;=5||maxx-x&lt;=5||maxy-y&lt;=5)
       gameover();
       for(i=1;i&lt;flag;i++)
       {
        if(arr[0][0]==arr[i][0]&amp;&amp;arr[0][1]==arr[i][1])
        gameover();
       }
      }
  void gameover()
      {textcolor(BLACK);
       clrscr();
       textcolor(WHITE);
       cout&lt;&lt;"YOU LOSE!\n";
       cout&lt;&lt;"GAME OVER\n";
       cout&lt;&lt;"Final Score = "&lt;&lt;sc;
       getch();
       getch();
       getch();
       getch();
       exit(1);
      }
  void main()
      {
       randomize();
       int gdriver = DETECT, gmode, errorcode;
       /* initialize graphics mode */
       initgraph(&amp;gdriver, &amp;gmode, "");
       maxx=500;
       maxy=400;
       x=maxx/2;
       y=maxy/2;
       ch='d';
       sc=0;
       if(introscreen())
       {
       textcolor(BLACK);
       clrscr();
       rectangle(0,0,maxx,maxy);
       flag=20;
       setcolor(GREEN);
       for(i=19;i&gt;=0;i--)
       {arr[i][0]=x-i;
        arr[i][1]=y-i;
        outtextxy(x-i,y-i,"X");
       }
       setcolor(WHITE);
       randomapple();
       while(1)
       {
          if(kbhit())
          {
              check=getch();
              switch(check)
              {
              case 'a':if(ch!='d') ch=check;
                  break;
              case 'd':if(ch!='a') ch=check;
                  break;
              case 'w':if(ch!='s') ch=check;
                  break;
              case 's':if(ch!='w') ch=check;
                  break;
              case ' ':ch=check;
                  break;
              }
  
          }
          condition();
          shift();
          printsnake();
          collision();
          score();
       }
       }
  
      }
  </code></pre>