Hi guys,

I need some help with sprintf. I get the general idea, and I think everything works, but my MessageBox will not diplay the right text.

int a  = 25;
char buf [80];
			
sprintf(buf, "%d", a );
MessageBox(NULL, (LPCWSTR)buf, L"Sel Change", MB_OK);

The cast to a wide string type should raise red flags. Perhaps you should consider wchar_t and swprintf from the get-go instead of trying to drop narrow characters into wide holes and hoping for magic.

You're right. I've tried a lot, from TCHAR to char* etc.

Could you post some code that would work?

*sigh*

int a = 25;
wchar_t buf[80];

swprintf(buf, L"%d", a);
MessageBox(NULL, buf, L"Sel Change", MB_OK);

Edited 6 Years Ago by Narue: n/a

Thanks man, it works perfectly!

I know I'm a little noob at this, but I just wanted to check i was doing the right thing. Otherwise it would take many posts if it didnt work ;)...

Cheers!

>I just wanted to check i was doing the right thing
Very rarely is there one "right thing". I can think of at least half a dozen ways to tackle the problem you were having, and I can guaran-damn-tee that kind of insight doesn't come from having other people solve your problems for you.

>Otherwise it would take many posts if it didnt work
And imagine how much you could have learned in that process that you have now missed out on...

Look, I've been making quite some programs lately, I've only started learning c++ 4 weeks ago, Win32 API about 1,5 weeks ago, so I am not an expert.

I've solved many harder problems though, and also with this one, I've been trying for quite some time. It was crutial to my program though, and I couldnt learn any more from it. I had to ask it ;).

Thanks anyway!

This question has already been answered. Start a new discussion instead.