Function not working in C++.

Started by
7 comments, last by eleusive 18 years, 6 months ago
Hi all, for some reason my function doesnt work. I tried everything, debugger wont say anything... Well, it works, but doesnt display everything.

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);
}


I just dont get it. I need to convert all that since messagebox wont accept strings darn it lol. Thanks.
-----------------------------....::::DRAGON BALL Z::::....C<<"+"<<"+"; // Go C++ !!!-----------------------------
Advertisement
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);}
"What are you trying to tell me? That I can write an O(N^2) recursive solution for a 2-dimensional knapsack?" "No, programmer. I'm trying to tell you that when you're ready, you won't have to." -Adapted from "The Matrix"
Try this:

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 :)

"What are you trying to tell me? That I can write an O(N^2) recursive solution for a 2-dimensional knapsack?" "No, programmer. I'm trying to tell you that when you're ready, you won't have to." -Adapted from "The Matrix"
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? :)
Thanks a lot, but I messed up, so I actually dont need that hehe sorry.
-----------------------------....::::DRAGON BALL Z::::....C<<"+"<<"+"; // Go C++ !!!-----------------------------
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.
-----------------------------....::::DRAGON BALL Z::::....C<<"+"<<"+"; // Go C++ !!!-----------------------------
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.
"What are you trying to tell me? That I can write an O(N^2) recursive solution for a 2-dimensional knapsack?" "No, programmer. I'm trying to tell you that when you're ready, you won't have to." -Adapted from "The Matrix"

This topic is closed to new replies.

Advertisement