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

Join the discussion
Ask a follow-up here, or get your own question answered by working scientists, mathematicians and engineers — people, not an autocomplete.
Real named experts · corrections over time · the nuance an AI answer skips
5 replies · 3K views
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
 
Physics news on Phys.org
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: