如何终止 Turbo C 中的无限循环?

发布于 2024-11-07 19:45:48 字数 292 浏览 0 评论 0原文

我陷入了无限循环。我怎样才能终止这个循环?我尝试使用/按 Cntrlc 但没有任何反应。我不知道如何阻止它。

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

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);
     }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

╰◇生如夏花灿烂 2024-11-14 19:45:48

CTRLBREAK 可能适用于此。我模糊地记得 CTRLC 并不总是适用于 Borland 产品。

不过,那是很久以前的事了,所以我必须从非常深刻的记忆中检索它,这些记忆可能已经有些褪色了:-)


我的问题是:为什么还有人在使用 Turbo C 时已经好多了还有同样便宜的解决方案吗?像 gcc(例如 Code::Blocks 中的)甚至 Microsoft Visual C Express。

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.

后来的我们 2024-11-14 19:45:48

你需要一个条件来打破你的 while 循环。

就像这样,

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

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

如果输入的字符是“q”,或者如果您坚持使用 while(1),则可以使用“break”关键字:

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;       

   }
}

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;       

   }
}
不及他 2024-11-14 19:45:48

如果您只想暂停 Turbo C 中的无限循环,请按 BREAK。如果您想返回 Turbo C 中的程序编辑器,请按 CTRL+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!

尐籹人 2024-11-14 19:45:48

CTRL-Break、Break 和 CTRL-C 对我不起作用,但 CTRL-ESC-ESC 对我有用! (这是在 Borland C++ 3.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).

谁人与我共长歌 2024-11-14 19:45:48

没有办法停止无限循环。但是,您可以在循环内部添加一个导致循环中断的条件,或者您可以在循环内部调用 exit() 函数,这将终止您的程序。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文