views:

224

answers:

5

I'm writing some code that returns an integer, which then needs to be outputted using printw from the ncurses library. However, since printw only takes char*, I can't figure out how to output it.

Essentially, is there a way to store a integer into a char array, or output an integer using printw?

A: 

Use itoa() or sprintf() to convert integer to ascii string.

Example:

char s[50];
sprintf(s, "%d", someInteger);

now u can pass s as char*

Pavel Radzivilovsky
Sorry, my answer is crap. Use Michael's.I just wasn't sure what ncurses printw does, so I wrote a workaround.
Pavel Radzivilovsky
You can delete answer. Several "bad" points: `itoa` -- there is no such thing in standard C (only `atoi`), it is better to use `snprintf(s,sizeof(s),"%d",someInteger)` -- safer.
Artyom
A: 

itoa will help you.

Li0liQ
+1  A: 

The itoa function converts an int to char*.

stiank81
+10  A: 

printw() accepts const char * as a format specifier. What you want is

printw("%d",yournumber);
Michael Krelin - hacker
+1 the right answer
dfa
out of curiosity, what exactly is "%d"?
Galileo
You might want to look up the printf manpage - http://linux.die.net/man/3/printf to learn the full power of formatting. in particular %d means signed integer as the first parameter after format string here. But that's not even the top of the iceberg ;-)
Michael Krelin - hacker
%decimal number
Kornel Kisielewicz
A: 

std::stringstream

This is not even close to a complete answer.
Roger Pate