Hi Guys,

Here's the situation that im stuck in.
I have 2 Header files which cannot be changed named bintree and bindnode. These define tree structure, aswell as several other functions eg inserting node, printing node etc

I have 2 Classes.

1 Class is :

class mazePoint
{int x;
char c;
}

Which has declare int x (for a character position) and char c;

2 Class is :

class mazeRow
{ bintree<mazePoint> row;
int y;}

Which has declared a tree of the mazepoints, with a int y for row number.

My problem is as follows : When i print the points out itself i get the characters each in a new line. This is because the print function (which cannot be changed) is as follows

void print() const {
	     if (left != NULL) left->print();
		 std::cout << nodeData.toString() << "\n";
		 if (right != NULL) right->print();

the "\n" makes a new line for each character so out come is as follows

#
#
#
#
#
#
#
s

#

and so forth - versus
#######s # (how it should be - a row)

this is becuase i am doing the toString function in mazePoint.

string mazePoint::toString() const
{
   stringstream line;
   string s;
   line << c;
   return line.str();
}

i tried move the toString function to mazeRow, but dont know what to call i.e in the above one it is using the char c (which stores the value).

I want to be able to print c as a row, rather than just print c.

Does anyone know how to do this.

I was thinking using the -> from mazeRow for int value y to a char to print.
But dont know how to do this.

Anyone have an idea. this is really important (for an assignment due soon).

Dani AI

Generated

As discovered, the shipped print routine writes each node then appends a newline, so a mazePoint that returns a single character shows up one-per-line. Two practical ways to get a single string per row without changing the library code:

  1. Capture the library output to a string, strip the per-node newlines, and print the cleaned row. This requires temporarily redirecting std::cout to an std::ostringstream, calling the tree's print, restoring std::cout, then removing '\n' from the captured string.
#include <sstream>
#include <algorithm>

std::string rowToString(bintree<mazePoint>& tree)
{
    std::ostringstream ss;
    auto* old = std::cout.rdbuf(ss.rdbuf());   // redirect cout -> ss
    tree.print();                              // library writes to cout
    std::cout.rdbuf(old);                      // restore cout
    std::string s = ss.str();
    s.erase(std::remove(s.begin(), s.end(), '\n'), s.end());
    return s;
}
  1. Avoid the library print entirely and build the row string by visiting nodes yourself. If bintree exposes iterators or a traversal callback (or a root pointer), walk the nodes in the same order the library uses and append characters to a std::string. This is preferable when available: it is faster, clearer, and thread-safe compared with redirecting std::cout.

Notes and cautions: redirecting std::cout is global and not thread-safe; restore the original buffer as soon as possible. If tree.print emits other text (row numbers, labels), simple newline removal may need to be replaced with a parser that extracts only the tile characters. Prefer a direct traversal/iterator approach if the bintree API supports it. See the C++ reference for stream buffering and stringstreams for implementation details: std::basic_ios::rdbuf, std::basic_stringstream, and std::remove.

I would like to add that, if i remove the "\n" (for testing purposes) it works correctly. therefore i am assuming, it is printing each row correctly, but for each char it is doing a further "\n".

I believe the solution is to use toString from row, and call the print() from mazePoint. But dont know how to do this at all.

The print function is stored with the binnode.h File.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.