-1

let say i'm in an infinite while loop and i want the user to enter the data in integer form. Then the data will be passed to another function for some other purpose this process will keep continuing until the user input esc in the place of data and i want the loop to break at that point. how should i do that?

 while(1)
 {
    printf("enter the data that need to entered\npress esc to exit\n");

    scanf("%d",&n);

    function(n);
 }

i tried to use if-else but if i put the ascii value of esc as the data input it exits the loop which i don't want to happen?

1 Answers1

0

As dingrite wrote in his other answer:

Your best bet is to create a custom "GetAsyncKeyState" function that will use #IFDEF for windows and linux to choose the appropriate GetAsyncKeyState() or equivalent.

No other way exists to achieve the desired result, the cin approach has its problems - such as the application must be in focus.

Take a look at this question: C++: execute a while loop until a key is pressed e.g. Esc?

Or you can try with this example found on other page

#include <stdio.h>

int main (void)
{
    int c;

    while (1) {
        c = getchar();            // Get one character from the input
        if (c == 27) { break; }  // Exit the loop if we receive ESC
        putchar(c);               // Put the character to the output
    }

    return 0;
}

Hope this helps.

Community
  • 1
  • 1
Markiian Benovskyi
  • 2,137
  • 22
  • 29