Home > OS >  How to change string on every iteration in ncurses widow?
How to change string on every iteration in ncurses widow?

Time:12-06

I need to make program which take value of pressed key and displays on window corresponding string. While user not press key enter, he can press keys arrow again and string will change every time after pressing. I tried to write this, but program don't work how I expect (When I press key right and then key left, there is piece of previous string).

#define _POSIX_C_SOURCE 200201L
#include <stdlib.h>
#include <curses.h>
#include <time.h>

int main () {
    srand(time(NULL));
    initscr();
    cbreak();
    noecho();

    // get screen sizes
    int yMax, xMax;
    getmaxyx(stdscr, yMax, xMax);

    //create a new window for input
    int height = 10;
    int width = 120;
    WINDOW * inputwin = newwin(height, width, yMax/2 - 5, (xMax/2 - width/2));
    box(inputwin, 0, 0);
    refresh();
    wrefresh(inputwin);

    // get amount rows
    echo();
    wmove(inputwin, 4, width/2 - 38);
    wprintw(inputwin, "Press key left to choose beginner level, key right - intermediate, key up - advances.");
    int amount_rows = 0;
    keypad(inputwin, TRUE);

    while (true) {
        move(6, 50);          // move to begining of line
        clrtoeol();          // clear line
        move(6, 50);          // move back to where you were
        wrefresh(inputwin);

        int c = wgetch(inputwin);

        if (c == KEY_LEFT) {
            mvwprintw(inputwin, 6, 50, "You chose beginner");
            amount_rows = 2;
        } else if (c == KEY_RIGHT) {
            mvwprintw(inputwin, 6, 50, "You chose intermediate");
            amount_rows = 3;
        } else if (c == KEY_UP) {
            mvwprintw(inputwin, 6, 50, "You chose advanced");
            amount_rows = 5;
        } else mvwprintw(inputwin, 6, 50, "INCORRECT INPUT. TRY AGAIN");

        wrefresh(inputwin);

        mvwprintw(inputwin, 7, 47, "Press enter to continue");
        int a = wgetch(inputwin);
        if (a == KEY_ENTER) break;
        wrefresh(inputwin);
    }


    // mvwprintw(inputwin, 7, 47, "Press enter to continue");
    // int a = wgetch(inputwin);
    // // if (a == KEY_ENTER) {
    // //     delwin(inputwin);
    // // }
    getch();
    endwin();

    return EXIT_SUCCESS;
}

When I press key right, it works as expected:

There is piece of previous string

CodePudding user response:

Try padding the messages with trailing spaces to erase longer messages.

#include <stdlib.h>
#include <ncurses.h>

int main ( void) {
    initscr ( );
    cbreak ( );
    noecho ( );

    // get screen sizes
    int yMax, xMax;
    getmaxyx ( stdscr, yMax, xMax);

    //create a new window for input
    int lines = ( yMax * 6) / 10;
    int columns = ( xMax * 8) / 10;
    WINDOW * inputwin = newwin ( lines
    , columns
    , ( yMax - lines) / 2
    , ( xMax - columns) / 2);
    box ( inputwin, 0, 0);
    keypad ( inputwin, TRUE);
    refresh ( );
    wrefresh ( inputwin);

    echo ( );
    wmove ( inputwin, 1, 1);
    wprintw ( inputwin, "Press key left to choose beginner level");
    wmove ( inputwin, 2, 7);
    wprintw ( inputwin, "key right - intermediate.");
    wmove ( inputwin, 3, 7);
    wprintw ( inputwin, "key up - advanced.");
    wmove ( inputwin, 7, 30);
    int choice = 0;

    while ( true) {
        wrefresh ( inputwin);

        int c = wgetch ( inputwin);

        if ( c == KEY_LEFT) {
            mvwprintw ( inputwin, 6, 5, "You chose beginner        ");
            choice = 2;
        } else if ( c == KEY_RIGHT) {
            mvwprintw ( inputwin, 6, 5, "You chose intermediate    ");
            choice = 3;
        } else if ( c == KEY_UP) {
            mvwprintw ( inputwin, 6, 5, "You chose advanced        ");
            choice = 5;
        } else if ( choice && ( c == KEY_UP || c == '\n')) {
            break;
        } else {
            mvwprintw ( inputwin, 6, 5, "INCORRECT INPUT. TRY AGAIN");
            choice = 0;
        }

        if ( choice) {
            mvwprintw ( inputwin, 7, 7, "Press enter to continue");
        } else {
            mvwprintw ( inputwin, 7, 7, "                       ");
        }
    }

    delwin ( inputwin);
    endwin ( );

    return EXIT_SUCCESS;
}
  • Related