Ncurses help

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

Post Reply
TheThirdL3g
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 14
Joined: Wed Apr 15, 2009 6:40 pm

Ncurses help

Post by TheThirdL3g »

Well I started learning c++ today after being inspired by them. I decided to begin with a console rpg. But after getting half the game done, I decided that I wanted a better console experience. I did some research and found ncurses. I installed and started playing around with it. This code has some sections with my just playing around (Like moving of the window with the arrows) but this is also going to be a future stat window that will pop up and close. I got it working but I can't display int variables in the window with wprintw(" "). I'm think its because I'm trying to display a int when it expects a string.
Here is my code:

Code: Select all

#include <ncurses.h>

WINDOW *create_newwin(int height, int width, int starty, int startx);
void destroy_win(WINDOW *local_win);
	
int main(int argc, char *argv[])
{	WINDOW *my_win;
	int startx, starty, width, height;
	int ch;
	int strength;
	int curlvl = 1;
	
	initscr();			/* Start curses mode 		*/
	cbreak();
	keypad(stdscr, TRUE);

	height = 3;
	width = 10;
	starty = (LINES - height) / 2;	/* Calculating for a center placement */
	startx = (COLS - width) / 2;	/* of the window		*/
	printw("Press F6 to exit");
	refresh();
	if (curlvl = 1){
		strength = 5;
	}
	my_win = create_newwin(height, width, starty, startx);
	while((ch = getch()) != KEY_F(6))
	{	switch(ch)
		{	case KEY_LEFT:
				destroy_win(my_win);
				my_win = create_newwin(height, width, starty,--startx);
				wprintw(my_win, "Hey");
				wrefresh(my_win);
				break;
			case KEY_RIGHT:
				destroy_win(my_win);
				my_win = create_newwin(height, width, starty,++startx);
				break;
			case KEY_UP:
				destroy_win(my_win);
				my_win = create_newwin(height, width, --starty,startx);
				break;
			case KEY_DOWN:
				destroy_win(my_win);
				my_win = create_newwin(height, width, ++starty,startx);
				break;	
			case KEY_F(5):
				destroy_win(my_win);
				break;
			case KEY_F(7):
				my_win = create_newwin(height, width, starty, startx);
				mvwprintw(my_win,1,1, strength);
				wrefresh(my_win);
				break;
		}
	}
	
	endwin();			/* End curses mode		  */
	return 0;
}

WINDOW *create_newwin(int height, int width, int starty, int startx)
{	WINDOW *local_win;

	local_win = newwin(height, width, starty, startx);
	box(local_win, 0 , 0);	

	wrefresh(local_win);	

	return local_win;
}

void destroy_win(WINDOW *local_win)
{	
	wborder(local_win, ' ', ' ', ' ',' ',' ',' ',' ',' ');
	wrefresh(local_win);
	delwin(local_win);
}
This is the error I get:

Code: Select all

invalid conversion from ‘int’ to ‘const char*’
initializing argument 4 of ‘int mvwprintw(WINDOW*, int, int, const char*, ...)’
User avatar
MarauderIIC
Respected Programmer
Respected Programmer
Posts: 3406
Joined: Sat Jul 10, 2004 3:05 pm
Location: Maryland, USA

Re: Ncurses help

Post by MarauderIIC »

http://linux.die.net/man/3/wprintw
The printw, wprintw, mvprintw and mvwprintw routines are analogous to
printf [see printf(3)].

http://linux.die.net/man/3/printf
http://www.cppreference.com/wiki/c/io/printf

In short, you're calling it wrong.

Edit: Sorry for the terse reply, but it's kind of late. Welcome to the forums! :D
I realized the moment I fell into the fissure that the book would not be destroyed as I had planned.
Post Reply