'Why the use of getch() shows error?

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

c


Solution 1:[1]

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).

Solution 2:[2]

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 !

Solution 3:[3]

I also have same problem. But _getche() instead of getch() had solved my problem.

Solution 4:[4]

getchar() might solve your problem.

Solution 5:[5]

It is contained in the conio.h library. So I think you should only add this line:

#include<conio.h>

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Some programmer dude
Solution 2 Liroo Pierre
Solution 3 Habib
Solution 4 Emma
Solution 5