objects within Vector, and creating recursion...

objects within Vectors, and creating recursion...

Page 1 of 1

3 Replies - 1241 Views - Last Post: 12 December 2010 - 06:13 PM Rate Topic: -----

#1 ryainad   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 24-November 09

objects within Vector, and creating recursion...

Posted 10 December 2010 - 07:31 PM

Hello!

I'm new in C++,
now I've done some code, it is working, but not properly...
I will try to explain it:

I attached initial design, where it says that I need to create vector contains 4 objects(forward, left, right, jump) and one more (repeat), which is should recursively create vector that contains previous 4 objects and so on.

My program creates vector which contains 4 objects. And now I have problem with Object (Repeat).

 class Prog{
private:

	vector<Node*> listing;
 .....
}
istream& operator >> (istream &infile, Prog &p){ ..... // Here I create vector cointains 4 objects and object Repeat
// how to create new vector that will contain previuos objects within "old" vector.
 }

Prog::Prog() {}
Prog::~Prog(){}


Attached File(s)



Is This A Good Question/Topic? 0
  • +

Replies To: objects within Vector, and creating recursion...

#2 janotte   User is offline

  • code > sword
  • member icon

Reputation: 991
  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

Re: objects within Vector, and creating recursion...

Posted 11 December 2010 - 12:30 AM

View Postryainad, on 11 December 2010 - 11:31 AM, said:

And now I have problem with Object (Repeat).


And what, exactly, is the problem you have with it?
Was This Post Helpful? 0
  • +
  • -

#3 ryainad   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 24-November 09

Re: objects within Vector, and creating recursion...

Posted 11 December 2010 - 09:16 AM

in my program there is statements_1: if LEFT - do left, if RIGHT - do right, if FORWARD - do forward, JUMP - do jump.
and it creates vector that contains that objects, but if REPEAT then the program should create new vector that will have statements_1.
I should use recursion ...
Was This Post Helpful? 0
  • +
  • -

#4 #define   User is offline

  • Duke of Err
  • member icon

Reputation: 1853
  • View blog
  • Posts: 6,671
  • Joined: 19-February 09

Re: objects within Vector, and creating recursion...

Posted 12 December 2010 - 06:13 PM

I've had a go at some of the code, the >> I take is just for the input.

#include <iostream>
#include <vector>
#include <string>
#include <fstream>


using namespace std;

class Node
{
  private:
    string str;

  public:
    Node() {str="default";}
    Node(string s) {str=s;}
    void show(void) {cout << str;};
};


class Prog
{
  private:
    vector<Node*> listing;

  public:
    ~Prog();
    void addNode(Node *n) { listing.push_back(n); };
    void display(void);
    friend istream& operator >> (istream &is, Prog &p);

};


void Prog::display(void)
{
  for(unsigned i=0; i< listing.size(); i++)
  {
    listing[i]->show();
    cout << endl;
  }
}

Prog::~Prog()
{
  for(unsigned i=0; i< listing.size(); i++)
  {
    delete listing[i];
  }
}


istream& operator >> (istream &is, Prog &p)
{
  Node* tmp;
  string str;

  for (int i=0; i<4 && is.good(); i++ )
  {
    is >> str;
    tmp = new Node(str);
    p.addNode(tmp);
  }

  return is;
}


int main()
{
  Node* tmp;
  Prog program;

  tmp = new Node("Left");
  program.addNode(tmp);

  tmp = new Node("Right");
  program.addNode(tmp);

  // use cin
  cout << "Enter four words please: ";
  cin >> program;

  // use a file stream
  ifstream fs;
  fs.open ("animals.txt");
  if (fs.is_open())
    fs >> program;
  fs.close();

  program.display();

  cin.ignore();
  cin.get();
  return(0);
}



I've taken some stuff out so hopefully this should compile.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1