Thread: How do I make a string buffer?

  1. #1
    Registered User
    Join Date
    Apr 2005
    Posts
    21

    How do I make a string buffer?

    First of all, I apologize if the title is misleading, I don't really know how to describe what I want to do in one line . I am working on a text console game. I am having some trouble implementing the combat messages. For those of you who have played any RPG games before you would have a good idea what the combat message window looks like. If not, here is a little explainations, I intend to have 4 lines of combat messages showing on the screen, the newest message would be pushed in at the bottom of the combat message box and effectively popping out the oldest message on the top. I think I know how to do that, I'll simply have 4 pointers and I will shift them up by one and have the pointer representing the newest line point to the new message string. So I am going to have a function that looks like

    void DrawCombatMsg(char *text);

    The argument would be the latest combat message, I don't know if passing it a cstring is the best way to do it but when I experimented with passing it a string class my compiler failed to recognize the function prototype in the header file It whined something about the identifier not found.

    But before I pass the latest combat message to my DrawCombatMsg() function I need to formulate the string (name of attacker, name of defender, hit or miss, how much damage). I have another function called

    FormCombatMsg()

    for that. Now this is where I am really confused. What kind of argument and return type should I put for FormCombat Msg function? Ideally I want it to return a string but the only way I know how to "form"(where I combine variables and strings) a string is by using cout and that that puts it directly to the standard output. So I guess my question is, is there a way that I can format a string and "save" it to another string so I can use it later on as an argument?

    I experimented with the string class of the string library and ostringstream of the sstream library, none of them really worked for me, most likely because I didn't use them right. Please help.

  2. #2
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    std::string

    Use it like any other datatype:

    Code:
    std::string S1;
    std::string S2 = "Stuff";
    std::string S3("Junk");
    std::string S4 = S3;
    
    std::string GetMoveCommand()
    {
      return "Move Left"; //Note: implicit conversion from char* to std::string
    }
    
    std::string S5 = GetMoveCommand();
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  3. #3
    Registered User
    Join Date
    Sep 2004
    Posts
    719
    how about something like

    Code:
    //untested
    using std::string;
    string GetMesg(...)  //   (...) = whatever you feel necessary (this will compile though)
    {
         string attacker = getAttacker();
         string side = getSide();
         string return_string += attacker + " is attacking you from the " + side;
    
         return return_string;
    }
    that function could really be summed up into one line, but, it's not..
    i seem to have GCC 3.3.4
    But how do i start it?
    I dont have a menu for it or anything.

  4. #4
    Registered User
    Join Date
    Apr 2005
    Posts
    21
    very interesting, I didn't know I can do arithematic operations on strings, didn't know they overloaded the operators for strings also. I'll have some fun experimenting with that one. Thanks

  5. #5
    Registered User
    Join Date
    Sep 2004
    Posts
    719
    Quote Originally Posted by pliang
    very interesting, I didn't know I can do arithematic operations on strings, didn't know they overloaded the operators for strings also. I'll have some fun experimenting with that one. Thanks

    it's not arithmatic

    well, deep deep down it is...but + simply concatenates 2 strings
    i seem to have GCC 3.3.4
    But how do i start it?
    I dont have a menu for it or anything.

  6. #6
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    I didn't know I can do arithematic operations on strings
    That's a standard string operation in a lot of languages. In C++, you can use + and [].

  7. #7
    Registered User
    Join Date
    Jan 2005
    Posts
    847
    If you want to use an array of chars you can then use sprintf like this.
    Code:
    sprintf(Buffer, "%s %s %s %d, Attacker, Defender, Hit, HitPointsLost);

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 11:03 PM
  2. RicBot
    By John_ in forum C++ Programming
    Replies: 8
    Last Post: 06-13-2006, 06:52 PM
  3. Compile Error that i dont understand
    By bobthebullet990 in forum C++ Programming
    Replies: 5
    Last Post: 05-05-2006, 09:19 AM
  4. can anyone see anything wrong with this code
    By occ0708 in forum C++ Programming
    Replies: 6
    Last Post: 12-07-2004, 12:47 PM
  5. Console Screen Buffer
    By GaPe in forum Windows Programming
    Replies: 0
    Last Post: 02-06-2003, 05:15 AM