How to Handle an Infinite Loop in C/C++ Code?

Click For Summary

Discussion Overview

The discussion revolves around handling infinite loops in C/C++ code, specifically focusing on exiting a loop when a specific key (the escape key) is pressed. Participants explore various methods to detect key presses and suggest alternatives for handling user input in console applications.

Discussion Character

  • Technical explanation
  • Debate/contested

Main Points Raised

  • One participant shares a code snippet that enters an infinite loop and asks how to exit it using the escape key.
  • Another participant suggests reading the keyboard buffer and breaking the loop if the escape character is detected.
  • A participant inquires about how to specifically check for the escape character.
  • Links to ASCII codes and functions like getch() and getchar() are provided as potential solutions for reading input.
  • A later reply notes that any code after the return(0) statement will not be executed and discusses alternative methods for terminating input in DOS console applications.

Areas of Agreement / Disagreement

Participants express differing views on the best approach to detect the escape key and handle the infinite loop, indicating that multiple competing views remain without a clear consensus.

Contextual Notes

There are limitations regarding the specific implementations of key detection across different operating systems, and the discussion does not resolve how to implement these methods effectively in all contexts.

Akshay_Anti
Messages
62
Reaction score
0
Hello there!

I typed this code and saved it as a .c file

Code:
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>

int main()
{
    int a;
    while(1)
    {
            if (kbhit())
            {
                        system("cls");
                        printf("%d ",getche());
            }
    }
    return 0;
    getch();
}

This code displays ASCII value of any character entered... The problem is that it being an infinite loop does not exit itself on pressing escape key.. How to handle that exception?
And what is the equivalent of kbhit() in C++ and in which header?

Thanks in advance
 
Technology news on Phys.org
you have to read the keyboard buffer if kbhit() returns 1 then if that character is an escape character then you can break; out of the loop.
 
i can check using if() condition, but how to check for escape character?
 
ok.. got it.. thanx.. :)
 
Note that the getch() or any code after the return(0) will never get used.

Normally to terminate user input in DOS like console windows, an "end of file" character is used, such as ctrl-D (hex 04) on Unix / Linux systems, and ctrl-Z (hex 1A) on MSDOS / Windows (in Dos console window) systems. For Windows / GUI type programs, ctrl-Z is normally the "undo" key.
 
Last edited:

Similar threads

  • · Replies 2 ·
Replies
2
Views
2K
Replies
1
Views
2K
  • · Replies 30 ·
2
Replies
30
Views
5K
Replies
14
Views
4K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 3 ·
Replies
3
Views
3K
  • · Replies 6 ·
Replies
6
Views
3K
Replies
47
Views
5K
Replies
81
Views
7K
  • · Replies 8 ·
Replies
8
Views
5K