Sign in to follow this  
powerskill

output to Screen Win32

Recommended Posts

I been looking for tutorials, but trust me when I say this. I can't even understand them, lots and lots of useless stuff they keep telling me to write and its not even what I want. Anyone know how to do something like. int x = 5; cout << x; of course this wont work because its Win32

Share this post


Link to post
Share on other sites
Depends what you want. If you want the value to appear in a pop-up dialog, use a MessageBox. If you want the value to appear in the client area of your window, you need to use TextOut() inside handling a WM_PAINT message.

Either way, you need to put your value in a string, for which you want an ostringstream:


#include <sstream>

using namespace std;

void f()
{
ostringstream os;

int x=5;

os << x; string s=os.str();

MessageBox(Hw,s.c_str(),"Output",MB_OK);
}


The above would require a non-UNICODE setting for the application BTW.

Share this post


Link to post
Share on other sites
I don't want a console application, I actually want a windows application.


EasilyConfused, what you gave me won't compile, are you sure its right or is it me thats messed up?

Share this post


Link to post
Share on other sites

#include <stdio.h> // For sprintf_s

const int MAX_TEXT = 100;

char text [ MAX_TEXT ];
int x = 5;

sprintf_s ( text, "%i", x );
MessageBox ( NULL, text, "Output", MB_OK );








Also, if you're using Visual Studio, be sure to set the Character Set to Multi-Byte in your project's properties. Hope this helps.

Share this post


Link to post
Share on other sites
If it won't compile, the solution is not to abandon std::stringstream and use sprintf_n. One day, MAX_TEXT will come back and bite you in the bottom.

What errors does my snippet give?


#include <string>
#include <sstream>

using namespace std;

void f(HWND Hw) // the HWND to your main window
{
ostringstream os;

int x=5;

os << x; string s=os.str();

MessageBox(Hw,s.c_str(),"Output",MB_OK);
}



As Darklighter says, you need to make sure UNICODE is not enabled for your project for the above to work.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this