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

AI Thread Summary
The discussion revolves around a C program designed to display the ASCII value of characters entered by the user. The main issue identified is that the infinite loop does not exit when the escape key is pressed. To resolve this, it is suggested to check for the escape character (ASCII code 27) using an if condition after confirming that a key has been pressed with kbhit(). The equivalent of kbhit() in C++ is also inquired about, along with the appropriate header file. The conversation highlights the use of getch() or getchar() for reading keyboard input, noting that any code following a return statement will not execute. Additionally, it is mentioned that in DOS console applications, user input can typically be terminated using an "end of file" character, such as ctrl-D or ctrl-Z, depending on the operating system.
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:
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I have a quick questions. I am going through a book on C programming on my own. Afterwards, I plan to go through something call data structures and algorithms on my own also in C. I also need to learn C++, Matlab and for personal interest Haskell. For the two topic of data structures and algorithms, I understand there are standard ones across all programming languages. After learning it through C, what would be the biggest issue when trying to implement the same data...
Back
Top