converting integers to strings
Is there an easy way to convert an integer value to a string so it can be printed? I am using the printw() functions in the ncurses library (C++) and they only accept strings as arguments.
|
In c++:
Code:
using namespace std; |
I tried what you said, but it didn't work. Here are the compiler errors:
Quote:
I did do #include <string>. |
You can either print the number directly:
Code:
printf("%d\n", num); Code:
{ |
sprintf function is a good way!
|
Ok, I'm going to try the sprintf() function looks like it will work. So, the question is, what arguments do I need to pass to it? I tried passing the single int value, which didn't work.
|
Code:
|
Thanks for the tip. Works great.
So, what does the "%d\n" do in the printw() function? Does it allow printing of multiple arguments or something? |
Quote:
Code:
int printw(const char *fmt, ...); |
Hi if you still want a funtion to convert an integer into string, you can use the function in the header file stdio.h.
char *ecvt(double number, int ndigits, int *decpt, int *sign); The ecvt() function converts number to a null-terminated string of ndigits digits (where ndigits is reduced to an system-specific limit determined by the precision of a double), and returns a pointer to the string. The high-order digit is nonzero, unless number is zero. The low order digit is rounded. The string itself does not contain a decimal point; however, the position of the decimal point relative to the start of the string is stored in *decpt. A negative value for *decpt means that the decimal point is to the left of the start of the string. If the sign of number is negative, *sign is set to a non-zero value, oth- erwise it’s set to 0. If number is zero, it is unspecified whether *decpt is 0 or 1. Wish you all the best. |
Quote:
error at line 91: to convert std::string to const char* call string::c_str() for a null terminated string or string::data() for a non-null terminated string. It sounds like you are using c and c-style strings so I would stick with one of the other suggestions. (this post is principally to correct errors should anyone else read it or use it for reference) |
Quote:
Code:
http://www.iota-six.co.uk/c/c2_printf_and_scanf.asp will shed more light on it as far as the '\n', look at http://www.liquifried.com/docs/useful/Cescapeseq.html |
Thanks!
Thanks for all the help! I went ahead and just printed the value using printw("%d\n",value). Since I had just been putting arguments into printw() like: printw("argument"), it wouldn't accept the integer with just printw(integer_value).
If anybody would like to see the fruits of this discussion, I posted the program and source available for download on my website. It is the ASCII Paddles game (at the bottom). FYI, The integer value that needed to be printed was the score, seen in the upper right corner. Thanks again! |
All times are GMT -5. The time now is 10:36 AM. |