1

I am making this simple program where I make a window and it using the arrow keys but cannot seem to find a solution. I am using the function mvwin() to move this window. All the inputs seem to reaching the window but there is no movement. All help would be appreciated.

Here is the code snippet:

#include<ncurses.h>

WINDOW *create_newwin(int height, int width, int starty, int startx)
{
      WINDOW *local_win = newwin(height, width, starty, startx);
      box(local_win, 0, 0);
      wprintw(local_win, "CAN YOU SEE ME MOVE");
      wrefresh(local_win);
      return (local_win);
}

int main()
{
       char ch;
       int x= 5, y= 10, h=5, wid= 20;
       initscr();
       noecho();
       //curs_set(FALSE);
       keypad(stdscr, TRUE);
       cbreak();
       refresh();

       printw("PRINT q to EXIT\n");
       WINDOW* wn= create_newwin(h, wid, x, y);                      //tried it directly with newwin() but nothing
       //box(wn,0,0);
       //wrefresh(wn);
       //keypad(wn, TRUE);
       //mvwin(wn, LINES/2, COLS/2);
       wrefresh(wn);

       while((ch=getch())!= 'q')                                      //function to move a window
       {
             switch(ch) 
             {
                    case KEY_LEFT: if(x>0)  --x;
                                    break;
                    case KEY_RIGHT: if(x<(COLS-16)) ++x;
                                    break;
                    case KEY_UP: if(y>0)    --y;
                                    break;
                    case KEY_DOWN: if(y<(LINES-6))  ++y;
                                    break;
            }
            //wprintw(wn,"%c",ch);                                  //to check for input
            mvwin(wn, y, x);
            wrefresh(wn);
    //      wclear(wn);
    }
    //wgetch(wn);
    delwin(wn);                                                     //clean up window
    endwin();
    return 0;
 }

1 Answer 1

3

You have an integer overflow in your switch statement.

Change your definition of char ch to int ch at the top of main and the problem goes away.

1
  • Thanks. I was using a,s,d,f to mimic the arrow keys. Is there a sure fire way to test/detect overflow? Or just need to be cautious and know your inputs? Also, the window displays my previous actions. How can I clear that and only print the present action? Feb 22, 2016 at 10:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.