Break out of infinit for loop for ~ input - C Statement

C examples for Statement:for

Description

Break out of infinit for loop for ~ input

Demo Code

#include <stdio.h>
void dropBomb(void);                        /* prototype */
int deaths;                                 /* global variable */
int main()//w  w  w .  j  a  v  a2s . co  m
{
   char x;
   deaths=0;
   for(;;)
   {
      printf("Press ~ then Enter to quit\n");
      printf("Press Enter to drop the bomb:");
      x=getchar();
      fflush(stdin);                    /* clear input buffer */
      if(x=='~')
      {
         break;
      }
      dropBomb();
      printf("%d people killed!\n",deaths);
   }
   return(0);
}
void dropBomb()
{
   int x;
   for(x=20;x>1;x--)
   {
      puts("* ");
   }
   puts("BOOM!");
   deaths+=1500;
}

Result


Related Tutorials