Juan
Juan

Reputation: 432

Printing characters with ncurses fails

I am using XUBUNTU 16.04 and Geany. I am starting to test the library ncurses. Bu I am not able to show characters. When I run the program a window appears, but the characters "a" and "*" does not appear.

This is my code:

#include <ncurses.h>
void line(char ch, int n)
{
 int i;
 for( i = 1; i<=n; i++ )
      addch(ch);
 }

 int main()
 {
  clear();
  line("a", 50);
  line("*", 8);
  return 0;
 }

Upvotes: 1

Views: 1513

Answers (2)

Thomas Dickey
Thomas Dickey

Reputation: 54583

Two things are missing:

  • initialization
  • something to keep the text on the screen momentarily

Initialization begins with initscr (or newterm, if you read the manual page). As written, the program would print something to the screen, and exit without pausing (and if your terminal uses the alternate screen, the text would vanish). A getch (read a character from the keyboard) does that, as well as doing refresh. By the way, the clear is unnecessary, because initscr does that:

The initscr code determines the terminal type and initializes all curses data structures. initscr also causes the first call to refresh(3x) to clear the screen. If errors occur, initscr writes an appropriate error message to standard error and exits; otherwise, a pointer is returned to stdscr.

Try this:

#include <curses.h>
void line(char ch, int n)
{
 int i;
 for( i = 1; i<=n; i++ )
      addch(ch);
 }

 int main()
 {
  initscr();
  cbreak();
  noecho();
  line("a", 50);
  line("*", 8);
  getch();
  endwin();
  return 0;
 }

Upvotes: 3

melpomene
melpomene

Reputation: 85887

man curs_refresh:

The refresh and wrefresh routines (or wnoutrefresh and doupdate) must be called to get actual output to the terminal, as other routines merely manipulate data structures.

So your program is missing a refresh();.

It also looks like you're missing initialization/cleanup, i.e. calling initscr() at the beginning and endwin() at the end of your program.

Upvotes: 3

Related Questions