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

In summary, the conversation discusses a code that displays the ASCII value of a character entered, but has an issue with the infinite loop not exiting on pressing the escape key. The solution is to check for an escape character and break out of the loop. The equivalent of kbhit() in C++ is getch() or getchar(), and an "end of file" character is used to terminate user input in console windows.
  • #1
Akshay_Anti
62
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
  • #2
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.
 
  • #3
i can check using if() condition, but how to check for escape character?
 
  • #5
ok.. got it.. thanx.. :)
 
  • #6
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:

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

What is the difference between C and C++ programming languages?

C and C++ are both programming languages used for system and application development. C is a procedural language, meaning it follows a linear and step-by-step approach to solving a problem. C++ is an object-oriented language, allowing for the creation of complex programs by using objects and classes.

What are the basic data types in C/ C++ programming?

The basic data types in C/ C++ include integers, characters, floating-point numbers, and void (which represents the absence of a data type). In addition, C++ also has the bool data type for boolean values.

What is the difference between declaring a variable and defining a variable in C/ C++?

Declaring a variable in C/ C++ simply means stating the variable's name and type, while defining a variable means assigning a value to it. For example, int num; is a declaration, while int num = 5; is a definition.

What is the purpose of using pointers in C/ C++ programming?

Pointers are variables that store the memory address of another variable. They are used to indirectly access and manipulate data stored in memory, making it easier to work with large data structures and improve efficiency.

What is the difference between pass by value and pass by reference in C/ C++ programming?

Pass by value means passing a copy of a variable's value to a function, while pass by reference means passing the variable's memory address to the function. This allows for direct manipulation of the original variable in pass by reference, while pass by value creates a copy of the variable which cannot be changed outside of the function.

Similar threads

  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
Replies
14
Views
2K
  • Programming and Computer Science
Replies
1
Views
954
  • Programming and Computer Science
Replies
4
Views
743
  • Programming and Computer Science
Replies
30
Views
2K
  • Programming and Computer Science
Replies
4
Views
3K
  • Programming and Computer Science
Replies
9
Views
1K
  • Programming and Computer Science
Replies
6
Views
2K
  • Programming and Computer Science
2
Replies
47
Views
4K
  • Programming and Computer Science
Replies
3
Views
3K
Back
Top