Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-

C to C++ strings- wrapping

Name: Anonymous 2007-03-23 1:52 ID:691HhKaB

Hey, I'm developing using Allegro, and it uses C-style strings, which is starting to become a pain. For example, I have code such as:

addMessage("%s %d", getName().c_str(), getAge());

addMessage is just a printf kind of function that uses variable arguments to create a string that will go to Allegro's textout, which takes a char * among other things. This works fine, but what I'd rather have is something like:

addMessage << getName() << " " << getAge();

So, I really just need to do something with strings and streams. Could someone help me out with what specifically needs to be done to implement this?

Name: Anonymous 2007-03-23 2:45 ID:5vQRFgWt

use stringstreams


#include <sstream>

std::ostringstream strout;
strout << "Name: " << getName() << " Age: " << getAge();
foo(strout.str().c_str());

Name: Anonymous 2007-03-23 2:46 ID:ZayS3vuO

>>1

while(1)
{
   break;
}

Name: Anonymous 2007-03-23 4:31 ID:Heaven

>>3's solution may work if you can't g

Name: Anonymous 2007-03-23 6:41 ID:Z60mEFpN

>>4
what an excellent idea. I have to get m

Name: Anonymous 2007-03-23 12:29 ID:ZNJZ8gv9

>>2
That looks like the right direction, and I'm thankful for your help. My question seemed a little incorrect from being written so late at night, though. I have an addMessage function that extracts the variable char * arguments given to it, and then it does:

messageQueue.push_back(newMessage);

messageQueue is just a list of strings. So, I don't really need to worry about the Allegro part at this point. Instead, I'd like to implement some kind of

foo << "---" << "etc";

foo needs to be able to push these things to the messageQueue, but with only one string per function call. Now that I think of it, is that even possible with streams?

Name: Anonymous 2007-03-23 12:57 ID:Xi9exePw

Considering that << is left associative, it's definitely possible. You'll want to look at cin's signature to get an idea how it's done. Something along the lines of overriding <<, accepting char * as an argument, and returning a copy of your object so that the next << can operate it.

Name: Anonymous 2007-03-23 12:58 ID:Xi9exePw

Actually, doesn't even need to be a copy.

Name: Anonymous 2007-03-23 13:05 ID:/S/tBbjI

>>7
So, it looks like I'll have to override char *, int, and whatever else I want it to handle automatically. That's fine, but I'm still not sure on how exactly to do it. Sure, I could just pass a stringstream to a function, but then I'd have something like:

oss << "Name: " << getName() << " Age: " << getAge();
addMessage(oss.str());
oss.str("");

That's three lines of code every time I want to add a message, though. Could you be a little more specific about what I need to do, maybe?

Name: Anonymous 2007-03-23 13:36 ID:SZEQogR8

goto fuck
fuck:
er, wat.

Name: Anonymous 2007-03-23 14:06 ID:gUgsQLR0

>>9
OH EXPLOITABLE

Name: Anonymous 2007-03-23 14:08 ID:/S/tBbjI

Okay, I got it, but thanks for your help. For reference, here's the rough draft:

class MessageHandler {
public:
    ostringstream moss;
    ostream& operator<< (char *val);
    void display();
};

ostream& MessageHandler::operator<<(char *rhs) {
    moss << rhs;
    return moss;
}

void MessageHandler::display() {
    cout << moss.str();
    moss.str("");
}

So, all I need is an instance of the message handler (or make them all static), and of course add ints and whatever else I want to have.

One more question, though. Why is it that I have to use ostringstream::str("") to clear its string? It seems like ostringstream::clear() doesn't do shit.

Name: Anonymous 2007-03-23 14:12 ID:oERjl27T

man, you gotta be a phd to do strings in C/C++

Name: Anonymous 2007-03-23 15:59 ID:qUWusmUp

>>13
What's an easier language for it? And don't say Scheme.

Name: Anonymous 2007-03-23 16:06 ID:huZE65en

scheme

Name: Anonymous 2007-03-23 18:51 ID:Heaven

>>13
bitc

Name: Anonymous 2009-08-16 22:58

Lain.

Name: Sgt.Kabuஓꦅkiman냺 2012-05-28 23:32

Bringing /prog/ back to its people
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy

Don't change these.
Name: Email:
Entire Thread Thread List