0

I am trying print the character ch at the cursor position in a 1x6 window. I want the cursor to move to the right if I input 's'/'k' and to the left if I input 'a'/'j'(and wrap around to the beginning/end of the window if the cursor is on the beginning/end position). However, waddch does not print ch at the cursor position. I tried using mvwaddch and change ch from type char to type chtype, and ch still is not shown at the cursor position. What am I missing so that ch is not printed?

chtype ch = 'X';
char ich;
int cols = 6;
int rows = 1;

WINDOW* win = newwin(rows, cols,  1, 1);
int delwin(WINDOW *win);
raw();


int currPos = 0;


initscr();

noecho();


for(int i = 0; i< 6; i++){


    ich = getch();

    if(ich == 'a' || ich == 'j'){
        //printf("\nsddss\n");

        //mvwaddch(win, 0,currPos - 1,ch);
        wmove(win, rows, currPos - 1);
        waddch(win, ch);
        currPos--;


    }else
    if(ich == 's' || ich == 'k'){
        //printf("sddss3333\n");
       //mvwaddch(win ,0,currPos + 1,ch);
        wmove(win, rows, currPos + 1);
        waddch(win, ch);
        currPos++;
    }
    wrefresh(win);
}

delwin(win);

endwin();
A.boj
  • 1
  • 3

1 Answers1

0

The wmove calls fail because you're passing a position outside the window. The coordinates start at zero, and giving it the number of rows puts that on the next line past the window.

A few other problems (the getch should read from the window win, and some other code was redundant. Here's a quick revision:

#include <curses.h>

int
main(void)
{
    chtype ch = 'X';
    int cols = 6;
    int rows = 1;
    int i;
    int currPos = 0;
    WINDOW *win;

    initscr();
    raw();
    noecho();

    win = newwin(rows, cols, 1, 1);

    for (i = 0; i < 6; i++) {
        int ich = wgetch(win);

        if (ich == 'a' || ich == 'j') {
            if (currPos > 0) currPos--; else beep();
        } else if (ich == 's' || ich == 'k') {
            if (currPos < cols - 1) currPos++; else beep();
        }
        wmove(win, 0, currPos);
        waddch(win, ch);
    }

    endwin();
    return 0;
}
Thomas Dickey
  • 51,086
  • 7
  • 70
  • 105