How to use Esc to stop a program in the middle of its C execution?

Asked

Viewed 438 times

2

I would like to stop the programme by pressing ESC at any time, as would be possible?

1 answer

0


With the use of the function getch you can pick up the character that was typed on the console, in which case the number 27 represents the keyboard ESC.

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

int main()
{
    char opcao;
    while(opcao != 27)
    {
        printf("\n\n\nEscolha uma opcao:\n");

        printf("<esc> para sair\n\n");

        opcao = getch();

        if (opcao != 27)
        {
            printf("Voce nao apertou ESC\n\n");
        }     
    }       
    return 0;
}
  • How would getch be "optional"? If I press nothing, it simply executes the next function. If I press, it aborts the program anywhere in it. Thank you

Browser other questions tagged

You are not signed in. Login or sign up in order to post.