Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why the use of getch() shows error?

Tags:

c

struct node
{
    int data;
    struct node *next;
}   *start=NULL;

void create()
{
    char ch;
    do
    {
        struct node *new_node,*current;

        new_node = (struct node *)malloc(sizeof(struct node));

        printf("\nEnter the data : ");
        scanf("%d",&new_node->data);
        new_node->next = NULL;

        if(start == NULL)
        {
            start = new_node;
            current = new_node;
        }
        else
        {
            current->next = new_node;
            current = new_node;
        }

        printf("nDo you want to creat another : ");
        ch = getch();
    } while(ch!='n');
}

This is the part of the code which contains getch()

When I try to run this code in an online compiler I am getting this error: undefined reference to getch collect2: error: 1d returned 1 exit status

How to solve this problem?...Please help

like image 860
Tannia Avatar asked Sep 19 '25 11:09

Tannia


2 Answers

There is no getch function in the standard C library, it only exist in Windows, and because it's not a standard function its real name is _getch (note the leading underscore). Judging by the error message your online compiler uses GCC, and most likely in a Linux environment, so no getch (or _getch) function.

If you want to be portable use fgetc or getc instead, or getchar (but note that those functions returns an int).

like image 175
Some programmer dude Avatar answered Sep 21 '25 11:09

Some programmer dude


There is a getch in linux, but in a library

#include <ncurses/curses.h>

See http://linux.die.net/man/3/getch for more detail. If the online compiler works under linux, and get the ncurses library, this will be work !

like image 22
Liroo Pierre Avatar answered Sep 21 '25 10:09

Liroo Pierre