#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    4
    Rep Power
    0

    Ncurses programming problem


    Hello everyone,

    i'm trying to learn ncurses on linux and i got stuck.
    I tried to figure out wat was wrong but scince i dont get anny errors it was hard to debug.

    can someone help me?
    I'm trying to put an X on te screen and move it aurond withde arrow keys.
    Code:
    #include <ncurses.h>
    int main(int argc, char *argv[])
    {	
            WINDOW *my_win;
    	initscr();
            raw();			
    	keypad(stdscr, TRUE);		
            noecho();
    
    	my_win = newwin(LINES, COLS, 0, 0);
            wprintw(my_win,"Test");
            wrefresh(my_win);
            int x = 5,y=5;
            int c;
      while(c = getch()!='q')
      {
        switch(c)
        {
          case KEY_DOWN:
            y--;
            break;
          case KEY_UP:
            y++;
            break;
          case KEY_LEFT:
            x--;
            break;
          case KEY_RIGHT:
            x++;
            break;
        }
        
        mvwaddch(my_win,x,y,0);
        wprintw(my_win,"X");
        wrefresh(my_win);
      }
      delwin(my_win);
      endwin();	
    	return 0;
    }
    Tanx
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    71
    Rep Power
    2
    Originally Posted by jorants
    Hello everyone,

    i'm trying to learn ncurses on linux and i got stuck.
    I tried to figure out wat was wrong but scince i dont get anny errors it was hard to debug.

    can someone help me?
    I'm trying to put an X on te screen and move it aurond withde arrow keys.
    Code:
    #include <ncurses.h>
    int main(int argc, char *argv[])
    {	
            WINDOW *my_win;
    	initscr();
            raw();			
    	keypad(stdscr, TRUE);		
            noecho();
    
    	my_win = newwin(LINES, COLS, 0, 0);
            wprintw(my_win,"Test");
            wrefresh(my_win);
            int x = 5,y=5;
            int c;
      while(c = getch()!='q')
      {
        switch(c)
        {
          case KEY_DOWN:
            y--;
            break;
          case KEY_UP:
            y++;
            break;
          case KEY_LEFT:
            x--;
            break;
          case KEY_RIGHT:
            x++;
            break;
        }
        
        mvwaddch(my_win,x,y,0);
        wprintw(my_win,"X");
        wrefresh(my_win);
      }
      delwin(my_win);
      endwin();	
    	return 0;
    }
    Tanx
    Your while loop, try something like so...

    Code:
    while((c = getch())!='q')/*Note the extra ()*/
    {/*...*/}
  4. #3
  5. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,893
    Rep Power
    481
    link to example of ncurses.
    [code]Code tags[/code] are essential for python code and Makefiles!
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    4
    Rep Power
    0
    Tank you! that fixed it.
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    4
    Rep Power
    0
    One other thing i tuned it a little bit, but somehow it prints "^@X" instead of just "X", any idiea why?

    tanx
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    4
    Rep Power
    0
    Oke, sorry for the extra post, but evrything is fixed, i missed a line where i printed 0 (not '0')

    tanx

IMN logo majestic logo threadwatch logo seochat tools logo