Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 464663
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T23:17:54+00:00 2026-05-12T23:17:54+00:00

everyone! I am new for C++ and now work on a C++ project.The whole

  • 0

everyone! I am new for C++ and now work on a C++ project.The whole structure has been completed.But I have been wondering about

how to build the vector of different objects and how to read the file since the beginning.

In my problem, first I have to read a txt file with readObstacles(std::istream &fs) that has the following format:

RECTANGLE 1 2.5 2 2 0.2
CIRCLE 1 2.5 1.2
RECTANGLE 4 2 2 2 0.3

(each obstacle begins a new line)

I need to read the data information of different obstacles and store these obstacles in a vector.
Class Obstacle is the base class with two sub-classes CIRCLE and RECTANGLE.
I try to put the these different obstacles (that i think should have their data info) in a obstacle vector and then call the virtual function they both have.

Below is the code I try to do with:

vector<Obstacle> obsdata; 

Myworld::readObstacles(std::istream &fs)
{
    std::string shape;
    double num1,num2,num3,num4,num5;
    while(fs>>shape>>num1>>num2>>num3>>num4>>num5)
    {
        if(shape=="CIRCLE") {
            CIRCLE c;
            c.m_Xc=num1;
            c.m_Yc=num2;
            c.m_Radius=num3;       
            obsdata.push_back(c);
        }

        if(shape=="RECTANGLE") {
            RECTANGLE r;
            r.center_x=num1;
            r.center_y=num2;    
            r.width=num3;
            r.height=num4;
            r.angle=num5;
            obsdata.push_back(r);
        }
    }
}

MyWorld::writeMatlabDisplayCode(std::ostream &fs)
{
    for( i = 0; i < obsdata.size(); i++ )
        obsdata[i].writeMatlabDisplayCode(fs);
}

I know it doesn’t work but i don’t know what i should do.

Any words will help. Thanks !

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T23:17:54+00:00Added an answer on May 12, 2026 at 11:17 pm

    You could try just reading the whole line at a time then tokenize the values…

    vector<Obstacle*> obsdata;
    
    string line;
    while(getline(fs, line)) {
        char *token = strtok(line.c_str(), " ");
        string shape(token);
    
        vector<double> numbers;
        stringstream ss;
        while(token = strtok(NULL, " ")) {
            double d;
            ss << token;
            ss >> d;
    
            numbers.push_back(d);
        }
    
        if(shape == "CIRCLE") {
            CIRCLE *c = new CIRCLE();
            c->m_Xc=numbers[0];
            c->m_Yc=numbers[1];
            c->m_Radius=numbers[2];       
            obsdata.push_back(c);
        } else if(shape == "RECTANGLE") {
            RECTANGLE *r = new RECTANGLE();
            r->center_x=numbers[0];
            r->center_y=numbers[1];    
            r->width=numbers[2];
            r->height=numbers[3];
            r->angle=numbers[4];
            obsdata.push_back(r);
        }
    }
    

    I would delete this vector of pointers in your MyWorld destructor:

    ~MyWorld() {
        // loop thanks to gf
        for(std::vector<Obstacle*>::iterator it = obsdata.begin(); it != obsdata.end(); ++it)
            delete *it;  
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

everyone! new to here and been pondering about this myself for some times with
Our team at work has enthusiastically adopted a rebase workflow, but we might have
Been using Django for < 1yr now, but this one is new-to-me. Hopefully someone?
I work on a site where none of us have used TFS before, but
I've been writing desktop apps in C# for some time now but I'm increasingly
Hello everyone I have an objective-c dilema :P I am quite new to objective-c,
Ok, I know this has been asked before (as I have viewed it myself)
This originally was a problem I ran into at work, but is now something
I've been developing GPL'd software for years, but now I need a more restrictive
I have a new laptop at work and code that worked earlier in the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.