Sign in to follow this  

Function not working in C++.

This topic is 4449 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

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.

Share this post


Link to post
Share on other sites
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);
}





Share this post


Link to post
Share on other sites
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? :)

Share this post


Link to post
Share on other sites
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. :)

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

This topic is 4449 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.

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