Appending/Concatenating strings

This is a discussion on Appending/Concatenating strings within the C++ Programming forums, part of the General Programming Boards category; I'm using a recursive implementation which has a method toString() that returns a string (concatenated or appended) by all the ...

  1. #1
    Registered User
    Join Date
    Sep 2007
    Posts
    119

    Appending/Concatenating strings

    I'm using a recursive implementation which has a method toString() that returns a string (concatenated or appended) by all the values in my tree. Unfortunately when I try using "+=" or the old "string = string + otherString" all it seems to do is override the original. Giving me a single value instead of all of them. When I replace it with "cout << " then all the values print. I have #include <string>, so I should be able to do that right?

    Code:
    //Note: toString() returns a string representation of any value being stored in the tree
    //          such as integers, floats etc... and works fine.
    string Node::print()
    {
    	string result;
    
            //travel left
    	if( this->getLeft() != NULL )
    	{
    		this->getLeft()->print();
    	}
    
            //Keep track of values
    	result += this->getValue()->toString(); //doesn't store all the values
    	//cout << this->getValue()->toString(); //prints all the values
    
           //travel right
    	if( this->getRight() != NULL )
    	{
    		this->getRight()->print();
    	}
    
    	return result;
    }

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    The trouble is that each instance of print() has its own result variable. You need to use only one variable, if I'm understanding you correctly. Perhaps passing one result as a reference might do the trick.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Shouldn't this:
    Code:
    this->getLeft()->print();
    be more like this:
    Code:
    result += this->getLeft()->print();

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,319
    >> The trouble is that each instance of print() has its own result variable.
    That shouldn't be a problem as long as the fix cpjust mentioned is implemented.

  5. #5
    Registered User
    Join Date
    Sep 2007
    Posts
    119
    oh, ha I suppose your right. Sometimes I don't analyze a recursive implementation as much as I would an iterative one. Thanks guys, works like a charm.

    What about casting into an object I created, like in java? Say you have a generic hierarchy item which you want to cast in order to use a specific class's methods? In java you just do (myObject)Node.getValue() and your done. Can I do that in C++?
    Last edited by John_L; 02-15-2008 at 06:21 PM.

  6. #6
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Yes, dynamic_cast. But most uses of that indicate a design problem.

    For data structures like trees or lists, which is what your code looks like, you'd use templates.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  7. #7
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by John_L View Post
    oh, ha I suppose your right. Sometimes I don't analyze a recursive implementation as much as I would an iterative one. Thanks guys, works like a charm.

    What about casting into an object I created, like in java? Say you have a generic hierarchy item which you want to cast in order to use a specific class's methods? In java you just do (myObject)Node.getValue() and your done. Can I do that in C++?
    Yuk! You don't really want to model your C++ programs after Java's sloppy style do you?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Strings Program
    By limergal in forum C++ Programming
    Replies: 4
    Last Post: 12-02-2006, 02:24 PM
  2. Programming using strings
    By jlu0418 in forum C++ Programming
    Replies: 5
    Last Post: 11-26-2006, 07:07 PM
  3. Reading strings input by the user...
    By Cmuppet in forum C Programming
    Replies: 13
    Last Post: 07-21-2004, 06:37 AM
  4. damn strings
    By jmzl666 in forum C Programming
    Replies: 10
    Last Post: 06-24-2002, 02:09 AM
  5. menus and strings
    By garycastillo in forum C Programming
    Replies: 3
    Last Post: 04-29-2002, 11:23 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21