void ShowScore(int p1, int p2)
{
stringstream *ss = new stringstream;
*ss<<p1;
string p1score;
*ss>>p1score;
delete ss;
ss = new stringstream;
*ss<<p2;
string p2score;
*ss>>p2score;
delete ss;
string message = "Your score: " + p1score + "\n\nAI score: " + p2score;
// ^^ ^^ ^^ ^^ ^^ ^^ ^^ ^^ ^^ ^^ ^^ Most likely its somewhere here
ss = new stringstream;
*ss<<message;
char all[101];
*ss>>all;
delete ss;
MessageBox(GetActiveWindow(),
all,
"Scores...",
MB_OK);
}
Function not working in C++.
Hi all, for some reason my function doesnt work. I tried everything, debugger wont say anything... Well, it works, but doesnt display everything.
I just dont get it. I need to convert all that since messagebox wont accept strings darn it lol. Thanks.
Why not just initially put everything into one stringstream? Also, you can use stringstream::str() to convert the stringstream to a string, then string::c_str() to convert the string to a char pointer.
void ShowScore(int p1, int p2){ stringstream message; message << "Your score: " << p1 << "\n\nAI score: " << p2; MessageBox(GetActiveWindow(), message.str().c_str(), "Scores...", MB_OK);}
Try this:
Edit: eleusive beat me to it. :)
void ShowScore(int p1, int p2){ stringstream ss; ss << "Your score: " << p1 << "\n\n" << "AI score: " << p2; MessageBox(GetActiveWindow(), ss.str().c_str(), "Scores...", MB_OK);}
Edit: eleusive beat me to it. :)
Quote:Original post by Jesse Chounard
Try this:
Edit: eleusive beat me to it. :)
Hehe, great minds think alike :)
Quote:Original post by dudedbz1
but doesnt display everything.
What exactly is missing? Is it possible that MessageBox doesn't handle newlines gracefully?
Anyway, simplicity is a virtue. The given example (ok, examples, but they're the same :) ) illustrate clean thinking with streams. In any case, you shouldn't dynamically allocate things when you don't need to. 'stringstream ss; ss<< whatever;' works just as well, and is much less hassle, yeah? :)
Quote:Original post by Zahlman
What exactly is missing? Is it possible that MessageBox doesn't handle newlines gracefully?
I believe what was happening is that it was breaking on the whitespace, and so the MessageBox would pop up with just "Your" in it.
Edit: And, no, MessageBox handles newlines just fine. :)
It actually handles nicely, and yes, the whitespace was messing up. Anyways it turns out I DID need it so I made my own solution which works now. And I'm using the heap from time to time cuz thats what I'm up to in the book I'm reading... just practice.
Quote:Original post by dudedbz1
It actually handles nicely, and yes, the whitespace was messing up. Anyways it turns out I DID need it so I made my own solution which works now. And I'm using the heap from time to time cuz thats what I'm up to in the book I'm reading... just practice.
Fair enough. However, one thing that is handy to know is when to and when to not use the heap. In this case, there is no tangible benefit from using the heap to create your stringstream.
In this case, it's not a big deal, but if this were a function that was called millions of time in a game, then you may see needless performance loss (allocating and deleting the stringstream over and over again). It's fine and all that you want to get practice with using the heap, but it's even better practice to find situations in which using the heap is beneficial. Not trying to say that using it in this situation is *bad*, I'm just giving you something to think about in the future.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement