DEV Community

Gurkirat Singh
Gurkirat Singh

Posted on • Updated on

Playing with NCurses Cursor (Part 2)

Hey there, if you haven't read Part-1 of this series, I would suggest you read it first.

In ncurses, a screen is described by x, y coordinated graph in the ncurses, and by default cursor is at (0, 0) or the top left corner.

To move cursor you should use move(int y, int x); function declared in ncurses.

For example → print hello world at y = 10, x = 20

move(10, 20);
printw("Hello World!!");
refresh(); // you should do this frequently, to flush changes from memory to screen
Enter fullscreen mode Exit fullscreen mode

The operation of move and printw is combined in one function is mvprintw

mvprintw(10, 20, "Hello World!");
refresh();
Enter fullscreen mode Exit fullscreen mode

Note: To clear screen you must use clear() function followed by refresh().

#include <ncurses.h>
using namespace std;

int main (int argc, char ** argv)
{
    initscr();

    // moving cursor, x = 20, y = 10
    move(10, 20);

    printw("I am here...");

    move(21, 10);
    printw("Now i am here");

    refresh();
    getch();
    endwin();
    return 0;
}
Enter fullscreen mode Exit fullscreen mode

Top comments (0)