Jump to content
  • Advertisement
Sign in to follow this  
BloodLust666

numbers to strings

This topic is 4749 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

How would i have a Message box display a number returned from a function? i.e. MessageBox(0, GetNum(), 0,0); i tryed putting (LPCSTR)GetNum() but that didn't work... any ideas?

Share this post


Link to post
Share on other sites
Advertisement
You'd have to specifically convert it yourself. One way is to do it like this (but likely many people are going to bug about it):

char szBuffer[512];
sprintf( szBuffer, "The number was %d.", GetNum() );

MessageBox( szBuffer, "Caption goes here", MB_OK );


Greetz,

Illco

Share this post


Link to post
Share on other sites
You can use either sprintf() or a string stream.

sprintf() is the C-way of doing this. If you are programming in C++ I suggest you to use string stream instead of sprintf().

with sprintf()

// a 32 bit integer contains at most 9 digits, the sign -,
// and we need some room for the '\0'
char buf[11];
sprintf(buf, "%d", myInteger);
MessageBox(0, buf, 0,0);

with string streams

#include <sstream>

std::string getNum(int myInteger)
{
std::stringstream myStream;
myStream << myInteger << std::ends;
return myStream.str();
}

MessageBox(0, getNum(myInteger).c_str(), 0, 0);


This question is probably in the top 5 FAQ. It might be a good idea to browse the forum before asking such simple question.

HTH,

Share this post


Link to post
Share on other sites
Using the c++ way is much safer.
Just a note on naming of functions: getNum(int) is terribly named.
Sorry, but I hate reading code like that. Name a function for what it does:
numToString(int)

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!