Click here to Skip to main content
15,867,308 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
public String toString()
{

    // Save the current position of the list
    int oldPos = currPos();
    int length = length();
    StringBuffer out = new StringBuffer((length() + 1) * 4);

    moveToStart();
    out.append("< ");
    for (int i = 0; i < oldPos; i++) {
      out.append(getValue());
      out.append(" ");
      next();
    }
    out.append("| ");
    for (int i = oldPos; i < length; i++) {
      out.append(getValue());
      out.append(" ");
      next();
    }
    out.append(">");
    moveToPos(oldPos); // Reset the fence to its original position
    return out.toString();
  }


What I have tried:

I cant convert it into c++ language, i tried anything and still can't get it.
Posted
Updated 10-Oct-21 20:58pm

I can't get exactly what you're doing, so I'm assuming that "next", "moveToPos" and so on are your object methods.

You should be able to convert your code simply changing StringBuffer and String into std::string or std::wstring and using += instead of out.append().

Maybe you're not doing the best C++ solution, but it should work.
 
Share this answer
 
This is not a code conversion service: we are not here to translate code for you.
Even if we did, what you would end up with would not be "good code" in the target language – they are based on very different frameworks, and what makes something work in one language does not always "translate" directly into another.
So what you end up with is very poor code, that is difficult if not impossible to maintain, that can’t be upgraded nicely, and that will cause you immense headaches if the original is changed. And it'll be a nightmare to debug if it doesn’t work "straight out of the box".
Instead, use the source code as a specification for a new app written in and for the target language / framework and write it from scratch using the original as a "template". You will get a much, much better result that will save you a lot of time in the long run.

And frankly? As I said last time you posted that code: it's bad code to start with! Conversion isn't going to improve that, and certainly isn't going to give you a good grade on your homework ...
 
Share this answer
 
Here you are a (oversimplified) working implementation. Now you should fill the details
C++
#include <iostream>
#include <sstream>
using namespace std;

const int POS = 8;


int getValue() { return rand(); }

string toString()
{
  ostringstream oss;
  oss << "< ";
  for (int i = 0; i < POS; i++)
  {
    oss << getValue();
    oss << " ";
  }

  return oss.str();
}

int main()
{
  cout << toString();
}
 
Share this answer
 
Comments
rooarrr 11-Oct-21 7:52am    
Thank you for this sir! but can you give me a guide or clue on the details? thank you

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900