🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

Silly Question

Started by
13 comments, last by ZomeonE 24 years, 5 months ago
How do I convert a int to LPCTSTR in c++? I want to print some values of int''s to debug my game using TextOut(hdc, x, y, LPCTSTR, strlen(LPCTSTR));
Advertisement
Create a function like this:

void val_to_str(unsigned long value,char *valstr){
unsigned char pos,r;
unsigned long tval;
tval=value;
pos=0;
do {
pos++;
tval=tval/10;
} while (tval>0);
for (r=0;r valstr[pos-r-1]=48+value%10;
value=value/10;
};
valstr[pos]=0;
};

Hope this helps
It nearly works. I get this error when I try to use the generated char (should it be a char?):


error C2664: ''Debug_Text'' : cannot convert parameter 1 from ''char'' to ''const char *''
Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast
Isn't there any easy way to print the value of a int on the screen?

Edited by - ZomeonE on 1/12/00 1:56:56 PM
stdarg.h library can help you.
The code would look something like this.

void Printf(char *text, ...)
{
char textBuf[1024];
va_list args;

va_start(text, args);
vsprintf(textBuf, text, args);
va_end(args);

TextOut(blah blah blah, textBuf, strlen(textBuf));
}

Now, you use it like standard printf function

int number = 5;
Print("number = %d", number);

Maybe I messed up somewhere, but hope this helps.
Hmmm.
If you''re willing to use CString (which is MFC but can be used independently of the massive MFC CObject heirarchy quite easily) all you have to do is use the CString::Format which works like a sprintf.
CString is fully interchangeable for LPCTSTR and const char*, so all you would have to do after using Format is replace the LPCTSTR with your CString.

-fel
You can use sprintf in the standard C library to build a
given string and then dislay it as such. For example, if you have an int var called x and a char array called buffer:

sprintf(buffer, "Value of x: %d", x);

Buffer now holds the string "Value of x: (whatver the value)" and it can be used as a char pointer variable to calls such as TextOut.

Why does this crazy thing lose my username and password half the time when I hit the post button?! *sigh*
-fel
~ The opinions stated by this individual are the opinions of this individual and not the opinions of her company, any organization she might be part of, her parrot, or anyone else. ~
Bah, that''s so old-school.

int x = 5;
strstream stream;
stream << x << endl;
cout << stream;

Actually I prefer the sprintf / Format way, but my "good C++ coder" conscience just won''t allow me to not mention strstream.



Mason McCuskey
Spin Studios - home of Quaternion, 2000 GDC Indie Games Fest Finalist!
www.spin-studios.com
Founder, Cuttlefish Industries
The Cuttlefish Engine lets anyone develop great games for iPad, iPhone, Android, WP7, the web, and more!
Thanks for all the help!
It''s helped me alot! =)

Which file should I include to use sprintf?
I get this error:

error C2065: ''sprintf'' : undeclared identifier

This topic is closed to new replies.

Advertisement