LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   converting integers to strings (https://www.linuxquestions.org/questions/programming-9/converting-integers-to-strings-213998/)

1337 Twinkie 08-05-2004 06:29 PM

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.

dakensta 08-05-2004 08:23 PM

In c++:

Code:

using namespace std;

template<typename numeric_t>
string
type_to_string(const numeric_t& n)
{
    ostringstream oss;
    oss << n;
    return oss.str();
}


1337 Twinkie 08-05-2004 08:57 PM

I tried what you said, but it didn't work. Here are the compiler errors:

Quote:

In function `std::string type_to_string(...)':
80: error: aggregate `std::ostringstream oss' has incomplete type and
cannot be defined
81: error: `n' undeclared (first use this function)
81: error: (Each undeclared identifier is reported only once for each
function it appears in.)
In function `void draw_score(int)':
91: error: cannot convert `std::string' to `const char*' for argument
`3' to `int mvprintw(int, int, const char*, ...)'
BTW, the integer value I am trying to change is a variable, not a constant. I don't know if it makes a difference.

I did do #include <string>.

itsme86 08-05-2004 09:10 PM

You can either print the number directly:
Code:

printf("%d\n", num);
Or you can put it in a buffer and then print it:
Code:

{
  char numstr[100]; // Or some other sufficiently large size

  sprintf(numstr, "%d", num);
  puts(numstr);
}

That's the C way to do it, but should work fine in C++ :)

james.zhang 08-05-2004 09:33 PM

sprintf function is a good way!

1337 Twinkie 08-05-2004 10:00 PM

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.

jhorvath 08-05-2004 10:37 PM

Code:


#include <ncurses.h>

int main() {

    initscr();

    int i = 23423;

    printw("%d\n",i);
    refresh();

    endwin();
    return 0;
}


1337 Twinkie 08-06-2004 12:13 AM

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?

barisdemiray 08-06-2004 01:51 AM

Quote:

Originally posted by 1337 Twinkie
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?

Do a search for the terms placeholder and escape sequences. First argument of printw isn't a string; it's a format string but `can be a string only'. As you will see in the man page prototype of printw is

Code:

int printw(const char *fmt, ...);
and three dots mean variable count of arguments. Finally, %d is the placeholder for integer type.

cppkid 08-06-2004 02:11 AM

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.

dakensta 08-06-2004 04:59 AM

Quote:

In function `std::string type_to_string(...)':
80: error: aggregate `std::ostringstream oss' has incomplete type and
cannot be defined
81: error: `n' undeclared (first use this function)
81: error: (Each undeclared identifier is reported only once for each
function it appears in.)
In function `void draw_score(int)':
91: error: cannot convert `std::string' to `const char*' for argument
`3' to `int mvprintw(int, int, const char*, ...)'
error at line 80: #include <sstream> // my fault, sorry

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)

jhorvath 08-06-2004 10:31 AM

Quote:

Finally, %d is the placeholder for integer type
si señor,... so for every variable you want to insert into the string that is inside the ()'s of printw you need a placeholder for it...

Code:


#include <ncurses.h>

int main() {

    initscr();

    int i = 23423;
    int j = 456;

    printw("variable 'i' = %d\nvariable 'j' = %d",i,j);
    refresh();

    endwin();
    return 0;
}

i made that hard to read on purpose ^^ ..you should see the effect of the '\n' in the code now..i put it in the last post out of habit, it was not necassary..

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

1337 Twinkie 08-06-2004 04:57 PM

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.