Hello every one,

i have written the following program for testing getch function behaviour.

but, as soon as i run the program i am getting segmentation fault.
i dont understand the problem.
i am using GCC compiler on Linux Environment.
i want to test the key scan program.

#include<ncurses.h>
int main()
{
        char ch;
        ch=getch();
        printf("%c",ch);
return 0;
}

running: ./a.out -lncurses

i have read the manual page of getch, where it is written that


The getch, wgetch, mvgetch and mvwgetch, routines read a character from the window. In no-delay mode, if no input is waiting, the value ERR is returned. In delay mode, the program waits until the system passes text through to the program. Depending on the setting of cbreak, this is after one character (cbreak mode), or after the first newline (nocbreak mode). In half-delay mode, the program waits until a character is typed or the specified timeout has been reached.

1.

The getch, wgetch, mvgetch and mvwgetch, routines read a character from the window.

can't i use getch to get a character from the keyboard.

please, some one give me a small example program on how to use
getch.


Thanks In Advance

Recommended Answers

All 5 Replies

Take a look at its prototype. What type does getch() return?

Take a look at its prototype. What type does getch() return?

int getch(void);
returns an integer value
so whats the problem, atleast it should print some value.

Do not forget to enter curses mode before using getch() and leave curses mode when you are done:

#include <ncurses.h>

int main()
{
    initscr();
    printw("Enter a character: ");
    printw("You entered '%c'\n", getch());
    refresh();
    endwin();

    return 0;
}

i have read in one of links, there its documented

The job of printw is to update a few flags and data structures and write the data to a buffer corresponding to stdscr. In order to show it on the screen, we need to call refresh() and tell the curses system to dump the contents on the screen.

but i am using following code

int main ()
 {
     char ch;
     initscr();
     printw("Enter a char :");
     ch=getch();
     printw("You Entered '%c' ",ch);
     getch();
     endwin();
     return 0;
}

the code does not have refresh(); but have one getch() extra
its showing out put on the window.

without that getch() the program is getting terminated in falsh so we cant observe the out put .

my question is how its printing o/p on window without refresh().

my question is how its printing o/p on window without refresh() .

In C++ output streams can be tied to input streams so that when a call is made to the input stream, the tied output stream flushes itself. Something like that might be happening here, but that is total speculation. I do not know the answer.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.