4

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);
     }
}
Jamal
  • 763
  • 7
  • 22
  • 32
Aaron
  • 1,969
  • 6
  • 27
  • 47
  • A portable version of your program would use `getchar()` rather than the non-standard `getche()`, and would store the result in an `int` rather than in a `char`. `int ch; while ((ch = getchar()) != EOF) { /* ... */ }` You also need `#include `, and `main()` should be `int main(void)`. (Possibly `getche()` does some things that `getchar()` doesn't; I'm not familiar with it.) – Keith Thompson Dec 03 '14 at 16:41

5 Answers5

8

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.

paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
  • C language is easy to learn when it comes to logic. You have to know the basic, since it was the origin of the c language, if you have mastered it, the new language that comes up will be easy for you to use. – Aaron May 17 '11 at 06:10
  • Can you give me any reason why I shouldn't use TurboC... I know this language is obsolete. – Aaron May 17 '11 at 06:35
  • 1
    @aerohn, that should be reason enough, I would think :-) The _language_ is not obsolete, though the Borland implementation is. It doesn't comply with the latest standards like gcc and it's matched in price by both the gcc and MSVC Express products. I prefer to use a more modern compiler myself. Keep in mind I'm not stating that you should consider moving away from C itself, just Turbo C. – paxdiablo May 17 '11 at 07:44
  • 1
    TurboC is a nice starter kit. I always liked the borland products. I think when you're in the early stages of learning the language, it doesn't matter if your compiler is 30 years old. – cidermonkey May 17 '11 at 21:15
  • In India, Turbo C++ 3/4.5 is the compiler 11th grade (High school) students are taught C++ in.. Too bad for those who use modern IDEs for the same. ;) – pradyunsg Sep 06 '14 at 10:38
  • Ctrl+ break is not working Turbo C++ 3.0. Break key paused my program. But ctrl + break is not working on window 7 dos emulator. @paxdiablo – Pie Dec 16 '19 at 07:07
1

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!

Dusty Campbell
  • 3,146
  • 31
  • 34
1

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).

Dave
  • 11
  • 1
1

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;       

   }
}
cidermonkey
  • 306
  • 1
  • 8
  • 3
    This would have worked, but I think the OP has already began running an infinite loop without a condition and wants to know how to exit the program and return to TurboC – adrian Aug 07 '17 at 04:14
-3

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.

Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574