convert int to string

Started by
26 comments, last by biscuitz 17 years, 8 months ago
I need help with stringstream method. I get error:
error: cannot convert `std::string' to `const CHAR*' for argument `4' to `BOOL TextOutA(HDC__*, int, int, const CHAR*, int)'

o.O "cannot convert" I've tried using &, typecasting... I never ran into something that "couldn't be converted" to another type
Advertisement
std::stringstream stream;stream<< my_int;std::string str = stream.str();// i dont use textout, so im not going to bother looking up the extra parameters// you probably have them right, the error specifically mentions parameter 4TextOut( one, two, three, str.c_str(), five );
:/ Well that got rid of the error but doesn't do what I expected... Oh, how do I delete/clear the stringstream?
Can't get any simpler than this:

int intValue = 4;
string strValue= "" + intValue;

[EDIT]
What would the disadvantage be if this appoarch was used instead of stringstream?

[Edited by - Deltasquadron2 on August 19, 2006 5:58:49 PM]
You can do something like this too:

char s[30];
int i=12345;

sprintf(&s[0], "%d", i);
You may also use sprintf (stdio.h) to convert an integer to a char*. An example of the use would be sprintf(str,"%d",100). If you use sprintf, please use bounded versions to prevent buffer overflows.

There is no reason to use non-standard solutions for this problem, so generally stringstream or sprintf/sscanf would be preferrable. If your project is in C++ and uses string instead of char*, stick with stringstream.
Quote:Original post by biscuitz
:/ Well that got rid of the error but doesn't do what I expected... Oh, how do I delete/clear the stringstream?


So what does it do? To clear a stringstream's contents you use str(""). To clear the status bits you use clear(). Depending on what you're doing with the stringstream you may need to use both.
Quote:Original post by Deltasquadron2
What would the disadvantage be if this appoarch was used instead of stringstream?


It just doesn't work. Try it.
:D tnx again SiCrane, str("") did the trick. I tried that sprintf approach, was printing out stuff like "null...error....invalid"

[EDIT]
:( That added like 500kb of memory and disk space just to put a number on the screen.

[EDIT2]
Using the stringstream method. If the mouse position goes over 3 digits, then goes below, it keeps the numbers on the end. I'm trying to figure out how to fix it. Also amazed at how complicated it is just to put a friggen variable on the screen with standard functions.

[Edited by - biscuitz on August 19, 2006 6:41:11 PM]
Quote:Original post by biscuitz
:D tnx again SiCrane, str("") did the trick. I tried that sprintf approach, was printing out stuff like "null...error....invalid"

[EDIT]
:( That added like 500kb of memory and disk space just to put a number on the screen.

[EDIT2]
Using the stringstream method. If the mouse position goes over 3 digits, then goes below, it keeps the numbers on the end. I'm trying to figure out how to fix it. Also amazed at how complicated it is just to put a friggen variable on the screen with standard functions.


You need to clear the area under text thats already on the screen to black ( or whatever background colour ).

Imagine I write the string 100 to the screen.

If I then write 33 at the same start position, the number will look like 330.

If you clear the area underneath first this wont happen.

You cannot "put a variable to the screen with standard functions." The C++ standard makes no provision for graphics, so functions such as TextOut are provided in a library.

500kb is around .001 % of my system ram, not including virtual ram, which is nothing. Your hard drive is even bigger.

This topic is closed to new replies.

Advertisement