Display an integer in a messagebox

I have spent an afternoon trying to figure this out. What I want to do is simple, display a number in a massage. ex.

1
2
MessageBox(hwnd, 40007/*just a random number*/, "My Number is:"
,MB_OK | MB_ICONINFORMATION);


I know this messagebox has to be Const char*| const char*| unit. so I thought I would just convert the integer to const char*, but it didn't work out =/

I don't really care how it happens, I just need to make a number appear in the damn box, but it may be subject to change so I can't just make a const char* variable and make it my number...

I'm not very experienced with application programming, and I'm sure it's not a very hard problem, but I need Help :)
Last edited on
1
2
3
4
5
std::string String ( double Val ) {
	std::ostringstream Stream;
	Stream << Val;
	return Stream.str ( );
}


That function will return a string containing the text-equivalent of the number. Just make sure you use MessageBoxA when using that function to obtain a string.

Example:
MessageBoxA ( NULL, String ( 500 ).c_str ( ), "Number", 0 );
Last edited on
Just make sure you use MessageBoxA

No.
Never call Ansi or Unicode apis directly.
and use wsprintf() or other to format a string.
and use wsprintf() or other to format a string.


wat
wsprintf is a Windows API function that was introduced in WIN16.

In WIN16 days, the hardware was very low spec and everything needed to make as small a footprint as possible. wsprintf doesn't format floating point numbers and as such, doesn't pull in the floating point library.

That was important at the time, less so now, these days there's little reason to use it.
No.
Never call Ansi or Unicode apis directly.

And why not? In most cases ANSI is supported on the OS anyways.
If you #undef UNICODE before #include <windows.h> you will get only the ...A functions
@NGen:
And why not? In most cases ANSI is supported on the OS anyways.

Code portability.

@george135:
Never call Ansi or Unicode apis directly.
and use wsprintf() or other to format a string.

wsprintf() is Unicode though.
Use _stprintf (#include <tchar.h> ).
1
2
3
4
5
6
7
8
9
10
#include <windows.h>
#include <tchar.h>

int PrintNumber(__in int nNumber)
{
  _TCHAR szBuffer[100];
  
  _stprintf(szBuffer, _T("%i"), nNumber);
  return MessageBox(hWnd, szBuffer, _T("wat"), MB_OK);
}

That'll show a message box with "10" in it.
@NGen:
And why not? In most cases ANSI is supported on the OS anyways.

Code portability.
Hmm, you can't embed quotes...

Yes, but in this case george135 is being his usual soap-box thumper with irrational gut responses. You cannot break "code portability" by using OS-specific APIs. There is absolutely nothing wrong with calling an ANSI or Unicode API directly when it is the Right Thing to do.

The STL doesn't play nicely with Unicode. And it certainly wasn't designed to work with Microsoft's existing UNICODE stuff. When you ask for a std::string from the STL, you are explicitly asking for non-unicode entities -- a flaw in the STL, perhaps, but that's the way things stand.

If you then compile with the UNICODE flags enabled, calling the MessageBox() macro with the standard string will definitely be the wrong thing to do, and your application will produce garbage and/or fail.

The correct thing to do is exactly as NGen reported: Use MessageBoxA() with std::string, and in so doing completely obviate all problems.

The MS Unicode handling is very robust and capable, and it is possible to combine the STL with it. You just have to use your mind about it. There are plenty of articles on the internet dealing with these very problems and the can of worms it opens. This is one of the best:
http://www.codeproject.com/KB/stl/upgradingstlappstounicode.aspx
(Hmm, there's also an old MS magazine article about this stuff I can't find...)

Further, there is nothing to fear when using the ANSI versions of functions over the wchar_t versions. In modern kernels, the worst is simply that the ANSI versions are stubs that call the wide versions. Boo hoo.
Topic archived. No new replies allowed.