Question

I get stuck in an infinite loop. How can I terminate this loop? I tried to use/press Cntrlc but nothing happens. I don't know how to stop it.

main()
{
     while (1)
     {
          char ch;
          printf("Enter a character: \n");
          ch = getche();
          printf("\nThe code for %c is %d.\n", ch, ch);
     }
}
Was it helpful?

Solution

CTRLBREAK will probably work for this. I have a vague recollection that CTRLC did not always work with the Borland products.

Though, that was a long time ago so I had to retrieve that from very deep memory, which may have faded somewhat :-)


My question for you is: Why is anyone still using Turbo C when much better and equally cheap solutions are available? Like gcc (such as in Code::Blocks) or even Microsoft Visual C Express.

OTHER TIPS

you need a condition to break out of your while loop.

so like,

main()
{
   char ch = ' ';
   while (ch != 'q')
   {

      printf("Enter a character: \n");
      ch = getche();
      printf("\nThe code for %c is %d.\n", ch, ch);
   }
}

would break out if the entered char was 'q', or if you insist on while(1), you could use the "break" keyword:

main()
{

   while (1)
   {
      char ch;
      printf("Enter a character: \n");
      ch = getche();
      printf("\nThe code for %c is %d.\n", ch, ch);

      if (ch == 'q')
         break;       

   }
}

If you want to just pause your infinite loop in Turbo C then press the BREAK. If you want to get back to the editor of your program in Turbo C then press CTRL+BREAK. It will return back to editing your program.

Yes, I tried this and it works!

CTRL-Break, Break and CTRL-C didn't work for me, but CTRL-ESC-ESC did! (This was tested with almost identical code in Borland C++ 3.1).

There is no way to stop an infinite loop. However, you can add a condition inside of the loop that causes it to break, or you can call the exit() function inside of the loop, which will terminate your program.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top