StudentLeftAlone
StudentLeftAlone

Reputation: 1

C How to exit from while loop with esc

I'd like to exit from while after typing 'esc' key into console. But unfortunatley i have no clue, how to do it without re-writing whole program.At the moment it exits the loop after ctrl+D.

    char* getUserInput(int bytes)
    {
      char* buffer = malloc(bytes);
      char* line = malloc(bytes);
      size_t len = 0;


      while (getline(&line, &len, stdin) > 0)  //I'd like to add one while condition
    //here, that will check if esc was pressed, like &&(_getch()!=27) 
    //or &&(!strcmp(line, (char)27)

      {
        strcat(buffer, line);
        line = malloc(bytes);
      }

      buffer[strlen(buffer) - 1] = '\0';
      return buffer;
    }

Upvotes: 0

Views: 1172

Answers (1)

Niloy Rashid
Niloy Rashid

Reputation: 707

See the code bellow. Hope it will help.

#include <stdio.h>

int main()
{
    char ch;
    do{
        ch = getch();
        printf("Inputed char: %c\n", ch);
    }while(ch != 27);
}

Upvotes: 2

Related Questions