Home > Software design >  ncurses printw wont print anything into window
ncurses printw wont print anything into window

Time:12-24

I am trying to create a window and print some text to it using ncurses but I am just getting a blank screen. I believe that printw is not working because I was able to open a working window with the same functions in the same order in a different program.

#include <stdio.h>
#include <stdlib.h>
#include <curses.h>
#include <unistd.h>

char string[] = "string";

int main(void)
{
    system("clear");
    int c;
    initscr();
    curs_set(0);
    noecho();
    keypad(stdscr, TRUE);
    cbreak();
    WINDOW *game_window;
    game_window=newwin(40,40,1,1);
    int num=0, highs=0;
    while (TRUE) {
        clear();
        printw("\t%s\n", string);
        printw("\tScore: %d    High Score: %d\n", num, highs);
        sleep(1);
        break;
    }
    endwin();
    printf("done\n");
    return 0;
}

CodePudding user response:

since you are printing this (I believe this is your intention) in game_window, use wprintw instead of printw:

wprintw(game_window, "\t%s\n", string);
wprintw(game_window, "\tScore: %d    High Score: %d\n", num, highs);

also, don't forget to that ncurses requires you to refresh windows:

refresh(); // main screen
wrefresh(game_window); // game_window window

That should help! Here's the whole code so you know where I placed above ;)

#include <stdio.h>
#include <stdlib.h>
#include <curses.h>
#include <unistd.h>

char string[] = "string";

int main(void)
{
    system("clear");
    int c;
    initscr();
    curs_set(0);
    noecho();
    keypad(stdscr, TRUE);
    cbreak();
    WINDOW *game_window;
    game_window=newwin(40,40,1,1);
    int num=0, highs=0;
    while (TRUE) {
        clear();
        wprintw(game_window, "\t%s\n", string);
        wprintw(game_window, "\tScore: %d    High Score: %d\n", num, highs);
        refresh();
        wrefresh(game_window);
        sleep(1);
        break;
    }
    endwin();
    printf("done\n");
    return 0;
}
  • Related