Help with ncurses (int to string)
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9

Thread: Help with ncurses (int to string)

  1. #1
    Join Date
    Apr 2009
    Posts
    12

    Help with ncurses (int to string)

    I'm playing around with ncurses and when I try to print a int variable I get this error:
    invalid conversion from ‘int’ to ‘const char*’
    initializing argument 4 of ‘int mvwprintw(WINDOW*, int, int, const char*, ...)’

    I think it is because I am trying to print a int when its asking for a string but I don't know what to do.

    This is my code:
    Code:
    #include <iostream>
    #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;
    	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);		/* Show that box 		*/
    
    	return local_win;
    }
    
    void destroy_win(WINDOW *local_win)
    {	
    	wborder(local_win, ' ', ' ', ' ',' ',' ',' ',' ',' ');
    
    	wrefresh(local_win);
    	delwin(local_win);
    }
    Last edited by toothyXdip; April 18th, 2009 at 04:35 PM.

  2. #2
    Join Date
    Nov 2006
    Location
    Australia
    Posts
    1,569

    Re: Help with ncurses (int to string)

    First, you search google. Then, search here on CodeGuru if google isn't fruitful. You may need to look at this function after you have found the solution int to string problem (of which there are many on google).
    Good judgment is gained from experience. Experience is gained from bad judgment.
    Cosy Little Game | SDL | GM script | VLD | Syntax Hlt | Can you help me with my homework assignment?

  3. #3
    Join Date
    Apr 2009
    Posts
    12

    Re: Help with ncurses (int to string)

    I already tried a some of them. Actually the first thing I did was searched "c++ int to string" but now I'm thinking it might be int to char so ill do some searching. I'll also look around on here some more.

  4. #4
    Join Date
    Nov 2006
    Location
    Australia
    Posts
    1,569

    Re: Help with ncurses (int to string)

    It won't be int to char if you want to store anything larger than the capacity of a char (255 unsigned).
    Good judgment is gained from experience. Experience is gained from bad judgment.
    Cosy Little Game | SDL | GM script | VLD | Syntax Hlt | Can you help me with my homework assignment?

  5. #5
    Join Date
    Apr 2009
    Location
    Russia, Nizhny Novgorod
    Posts
    99

    Re: Help with ncurses (int to string)

    boost::lexical_cast is the best way in such a conversion
    http://www.boost.org/doc/libs/1_38_0...xical_cast.htm
    also you can use standard C++ ways like sprintf or std::stringstream, but using boost will be more elegant.

  6. #6
    Join Date
    Apr 2009
    Posts
    12

    Re: Help with ncurses (int to string)

    I installed boost from this site: http://boost.darwinports.com/

    This sounds really stupid but using boost.h as the header file doesn't work. Nor does <boost/(anyname).hpp>. What is the header file name?

    I'm compiling with g++ Test.cpp -lncurses -o Test
    Last edited by toothyXdip; April 18th, 2009 at 04:35 PM.

  7. #7
    Join Date
    Apr 2009
    Location
    Russia, Nizhny Novgorod
    Posts
    99

    Re: Help with ncurses (int to string)

    toothyXdip, It looks like your compiler doesn't know where the boost is placed. Try adding the path to the boost in property of your compiler or use the full path.

  8. #8
    Join Date
    Apr 2009
    Posts
    12

    Re: Help with ncurses (int to string)

    I searched around and found a way to do it without boost until I get it worked out (I will look where it was installed now).

    Using this header file ("convert.h"):
    Code:
    #include <iostream>
    #include <sstream>
    #include <string>
    #include <stdexcept>
    
    class BadConversion : public std::runtime_error {
    public:
      BadConversion(const std::string& s)
        : std::runtime_error(s)
        { }
    };
    
    inline std::string stringify(double x)
    {
      std::ostringstream o;
      if (!(o << x))
        throw BadConversion("stringify(double)");
      return o.str();
    }
    I used the stringify (commented below) and changed my int to a double:
    Code:
    #include <iostream>
    #include <ncurses.h>
    #include "convert.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;
    	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, stringify(strength)); // <------ Stringify
    				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);		/* Show that box 		*/
    
    	return local_win;
    }
    
    void destroy_win(WINDOW *local_win)
    {	
    	wborder(local_win, ' ', ' ', ' ',' ',' ',' ',' ',' ');
    
    	wrefresh(local_win);
    	delwin(local_win);
    }
    I get this error:
    Code:
    NCWin.cpp: In function ‘int main(int, char**)’:
    NCWin.cpp:59: error: cannot convert ‘std::string’ to ‘const char*’ for argument ‘4’ to ‘int mvwprintw(WINDOW*, int, int, const char*, ...)’
    So it did covert it to a string but it seems as I thought before it is asking for a char. Maybe...
    Last edited by toothyXdip; April 19th, 2009 at 12:23 AM.

  9. #9
    Join Date
    Apr 2009
    Location
    Russia, Nizhny Novgorod
    Posts
    99

    Re: Help with ncurses (int to string)

    std::string has the c_str() member which returns the const char*

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center