0

I'm trying to make a simple ncurses program to display a box with messages. Im following this link and it works well. The problem that I'm having is that if I put the whole code in a function and call it in a loop, the initialisation is having an error. As far as I know, if I called endwin() at the end of the function, there shouldn't be any problem calling the initscr() again. Am I missing a function to enable the initscr() to be called again?

This is the code:

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

int call()
{
    WINDOW *mainwin, *childwin;
    int ch;

    if ((mainwin = initscr()) == NULL) {
            fprintf(stderr, "Error initialising ncurses.\n");
            exit(EXIT_FAILURE);
    }

    noecho();
    curs_set(FALSE);

    keypad(mainwin, TRUE);

    mvaddstr(childwin, 1, 6, "Warning! Press q to exit");
    mvaddstr(childwin, 2, 15, "{ OK }");
    refresh();
    while( (ch = getch()) != 'q') {
            refresh();
    }

    delwin(mainwin);
    endwin();
    refresh();

    return EXIT_SUCCESS;
}

int main()
{
    int i;

    for (i=0;i<10;i++) {
            call();
    }

    return 0;
}

EDIT: I've put the header file. Change the code to be more simpler

Mohd Fikrie
  • 197
  • 4
  • 21

1 Answers1

2

The sample program does not call initscr more than one. However, it does not show the #include lines either (necessary to compile), so perhaps it is not the program which was actually run.

Regarding a problem calling initscr (once or more than once), there are at least two possibilities:

  • you are using some other implementation of curses (e.g., a Unix one such as HPUX). As noted in X/Open, portable programs must not call initscr more than once.
  • the TERM variable is not set to a usable value. As usual, the manual page explains what the function does, and why it might fail.
Thomas Dickey
  • 51,086
  • 7
  • 70
  • 105
  • the intscr is in a function and I'm calling that function repeatedly. Doesn't that mean initscr is called more than once? Anyway, the things that you pointed out did help me on solving this problem. Thanks a lot! – Mohd Fikrie Apr 17 '15 at 01:58