Input functions

NCURSES Programming HOWTO
Prev |  | Next

# 7. Input functions

Well, printing without taking input, is boring. Let's see functions which allow us to get input from user. These functions also can be divided into three categories.

1. getch() class: Get a character

2. scanw() class: Get formatted input

3. getstr() class: Get strings

## 7.1. getch() class of functions

These functions read a single character from the terminal. But there are several subtle facts to consider. For example if you don't use the function cbreak(), curses will not read your input characters contiguously but will begin read them only after a new line or an EOF is encountered. In order to avoid this, the cbreak() function must used so that characters are immediately available to your program. Another widely used function is noecho(). As the name suggests, when this function is set (used), the characters that are keyed in by the user will not show up on the screen. The two functions cbreak() and noecho() are typical examples of key management. Functions of this genre are explained in the key management section .

## 7.2. scanw() class of functions

These functions are similar to `scanf()` with the added capability of getting the input from any location on the screen.

### 7.2.1. scanw() and mvscanw

The usage of these functions is similar to that of `sscanf()`, where the line to be scanned is provided by `wgetstr()` function. That is, these functions call to `wgetstr()` function(explained below) and uses the resulting line for a scan.

### 7.2.2. wscanw() and mvwscanw()

These are similar to above two functions except that they read from a window, which is supplied as one of the arguments to these functions.

### 7.2.3. vwscanw()

This function is similar to `vscanf()`. This can be used when a variable number of arguments are to be scanned.

## 7.3. getstr() class of functions

These functions are used to get strings from the terminal. In essence, this function performs the same task as would be achieved by a series of calls to `getch()` until a newline, carriage return, or end-of-file is received. The resulting string of characters are pointed to by `str`, which is a character pointer provided by the user.

## 7.4. Some examples

Example 4. A Simple scanw example

    #include <ncurses.h>			/* ncurses.h includes stdio.h */
    #include <string.h>

    int main()
    {
     char mesg[]="Enter a string: ";		/* message to be appeared on the screen */
     char str[80];
     int row,col;				/* to store the number of rows and *
    					 * the number of colums of the screen */
     initscr();				/* start the curses mode */
     getmaxyx(stdscr,row,col);		/* get the number of rows and columns */
     mvprintw(row/2,(col-strlen(mesg))/2,"%s",mesg);
                         		/* print the message at the center of the screen */
     getstr(str);
     mvprintw(LINES - 2, 0, "You Entered: %s", str);
     getch();
     endwin();

     return 0;
    }

Prev | Home | Next
Output functions |  | Attributes