5 Replies - 340 Views - Last Post: 12 May 2014 - 11:24 AM Rate Topic: -----

#1 jack-o  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 12-May 14

problem iterating through and erasing vector objects

Posted 12 May 2014 - 08:12 AM

Hi,
I know this is not a novel problem, but try as I might, I've been unable to make this code work following examples found online.
I am new to using vectors and decided to create a series of increasingly challenging problems for myself ( documented on ayearofcode.wordpress.com). I haven't gotten to far, but I have been able to create objects and place them in a vector and then iterate through making comparisons.
However, I have NOT been able to erase an object from my vector following the comparison.

The basic problem is to create a clown car (my vector), stuff it full of clowns (my objects), search the car for a clown (I'm going by ID Number, e.g. 'X') and kick that clown out of the car. Then I go back and print the whole vector, which should show that clown X is now missing.

Thank you in advance for any help!!

Here's my code, with my output following:

//
//  main.cpp
//  clowncar
//
//  Created by Jack on 5/4/14.
//  Copyright (c) 2014 Jack Treml. All rights reserved.
//



/*
 1. empty car (0)
 
 2. pick up Pennywise and John Wayne Gacy (2)
 
 3. Pick up Bozo and Krusty (4)
 
 4. Pick up Ronald McDonald (5)
 
 5. print out list of clowns
 
 6. As for a name of a clown (or ID number) to kick out of the car
 
 7. Kick only that individual out, then re-print list
 
 */

#include <iostream>
#include <vector>
using namespace std;
using std::vector;

class IdNumbers{     // a place to store a counter for creating idNumbers
public:
    IdNumbers(){
        
    }
    
    static int getANewNumber(){
        counter++;
        return counter;
    }
   
protected:
     static int counter;
};

class Clown{   // the clown objects
public:
    Clown(){};
    ~Clown(){};
    
    Clown(int clownId){
        idNum = clownId;
    };
    Clown(string nameOfClown, IdNumbers theNumber){
        clownName = nameOfClown;
        idNum = theNumber.getANewNumber();
        
    };
    int getClownId(void){
        return idNum;
    }
    void printClown(void){
        cout<<"idNumber = "<<idNum<<endl;
        cout<<"name = "<<clownName<<endl;
        
    };
    
    void printClown2(void){
        cout<<"Clown idNumber = "<<idNum<<", name = "<<clownName<<endl;
        
    };
    
private:
    string clownName;
    int idNum;
    
};

//functions
void printVectorContents(vector<Clown>);
void printVectorContents2(vector<Clown>);
void askWhoToPop(vector<Clown>);
void popClown(vector<Clown>, int);
void popThisClown(vector<Clown> car, int popWho);

int IdNumbers ::counter = 0;

int main(int argc, const char * argv[])
{
    IdNumbers theCounter;
    
    vector<Clown> clowncar;     //sets up a vector for fish
    int vectorSize = 0;
    vectorSize = clowncar.size(); //get array size
    cout<<"**************"<<endl;
    cout<<"vector is size: "<<vectorSize<<endl;
    Clown pennywise("Pennywise", theCounter);
    clowncar.push_back(pennywise);
    Clown jwgacy("John Wayne Gacy", theCounter);
    clowncar.push_back(jwgacy);
    vectorSize = clowncar.size(); //get array size
    cout<<"**************"<<endl;
    cout<<"vector is size: "<<vectorSize<<endl;
    //printVectorContents(clowncar);
    printVectorContents2(clowncar);
    
    Clown bozo("Bozo", theCounter);
    clowncar.push_back(bozo);
    Clown krusty("Krusty", theCounter);
    clowncar.push_back(krusty);
    vectorSize = clowncar.size(); //get array size
    cout<<"**************"<<endl;
    cout<<"vector is size: "<<vectorSize<<endl;
    //printVectorContents(clowncar);
    printVectorContents2(clowncar);
    
    Clown ronald("Ronald McDonald", theCounter);
    clowncar.push_back(ronald);
    vectorSize = clowncar.size(); //get array size
    cout<<"**************"<<endl;
    cout<<"vector is size: "<<vectorSize<<endl;
    //printVectorContents(clowncar);
    
    //list clowns with ID numbers
    printVectorContents2(clowncar);
    //ask for an ID number of a clown to evict
    askWhoToPop(clowncar);
    
    
    cout<<"**********"<<endl;
    cout<<"vector is size: "<<vectorSize<<endl;
    printVectorContents2(clowncar);
    
    
    /*                                  This is an output section from a previous project
    clowncar.pop_back();
    vectorSize = clowncar.size(); //get array size
    cout<<"**************"<<endl;
    cout<<"vector is size: "<<vectorSize<<endl;
    //printVectorContents(clowncar);
    printVectorContents2(clowncar);
    
    while(clowncar.size() > 0){
        clowncar.pop_back();
    }
    vectorSize = clowncar.size(); //get array size
    cout<<"**************"<<endl;
    cout<<"vector is size: "<<vectorSize<<endl;
    //printVectorContents(clowncar);
     */
    
    
    return 0;
}


//Non-Member functions
void printVectorContents(vector<Clown> car){
    for(int i=0; i < car.size(); i++){
        car[i].printClown();
        
    }
}

void printVectorContents2(vector<Clown> car){
    for(int i=0; i < car.size(); i++){
        car[i].printClown2();
    }
}

void askWhoToPop(vector<Clown> car){
    int popWho;
    cout<<"Please enter an ID number of a clown to kick out of the car: ";
    cin>>popWho;
    popThisClown(car, popWho);
    
    cout<<endl;
    
}

void popThisClown(vector<Clown> car, int popWho){   //how to selectively delete from a vector????
    for (int j = 0; j < car.size(); j++)
    {
        cout<<"is "<<j<<" the right guy to bump?"<<endl;
        if(car[j].getClownId() == popWho)
        {
            cout<<"yeah, I'm here lookin' at erasing clown "<<j<<endl;
            //car.erase(j);   //tried this - didn't work
            car.erase (car.begin()+j);
        }
    }// end for
};




output:
**************
vector is size: 0
**************
vector is size: 2
Clown idNumber = 1, name = Pennywise
Clown idNumber = 2, name = John Wayne Gacy
**************
vector is size: 4
Clown idNumber = 1, name = Pennywise
Clown idNumber = 2, name = John Wayne Gacy
Clown idNumber = 3, name = Bozo
Clown idNumber = 4, name = Krusty
**************
vector is size: 5
Clown idNumber = 1, name = Pennywise
Clown idNumber = 2, name = John Wayne Gacy
Clown idNumber = 3, name = Bozo
Clown idNumber = 4, name = Krusty
Clown idNumber = 5, name = Ronald McDonald
Please enter an ID number of a clown to kick out of the car: 1
is 0 the right guy to bump?
yeah, I'm here lookin' at erasing clown 0
is 1 the right guy to bump?
is 2 the right guy to bump?
is 3 the right guy to bump?

**********
vector is size: 5
Clown idNumber = 1, name = Pennywise
Clown idNumber = 2, name = John Wayne Gacy
Clown idNumber = 3, name = Bozo
Clown idNumber = 4, name = Krusty
Clown idNumber = 5, name = Ronald McDonald
Program ended with exit code: 0

Is This A Good Question/Topic? 0
  • +

Replies To: problem iterating through and erasing vector objects

#2 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3103
  • View blog
  • Posts: 10,898
  • Joined: 08-August 08

Re: problem iterating through and erasing vector objects

Posted 12 May 2014 - 09:11 AM

You're working on copies of the vector. You need to pass by reference so that you only work on the original.

//
//  main.cpp
//  clowncar
//
//  Created by Jack on 5/4/14.
//  Copyright (c) 2014 Jack Treml. All rights reserved.
//



/*
 1. empty car (0)

 2. pick up Pennywise and John Wayne Gacy (2)

 3. Pick up Bozo and Krusty (4)

 4. Pick up Ronald McDonald (5)

 5. print out list of clowns

 6. As for a name of a clown (or ID number) to kick out of the car

 7. Kick only that individual out, then re-print list

 */

#include <iostream>
#include <vector>
using namespace std;
using std::vector;

class IdNumbers{     // a place to store a counter for creating idNumbers
public:
	IdNumbers(){

	}

	static int getANewNumber(){
		counter++;
		return counter;
	}

protected:
	static int counter;
};

class Clown{   // the clown objects
public:
	Clown(){};
	~Clown(){};

	Clown(int clownId){
		idNum = clownId;
	};
	Clown(string nameOfClown, IdNumbers theNumber){
		clownName = nameOfClown;
		idNum = theNumber.getANewNumber();

	};
	int getClownId(void){
		return idNum;
	}
	void printClown(void){
		cout<<"idNumber = "<<idNum<<endl;
		cout<<"name = "<<clownName<<endl;

	};

	void printClown2(void){
		cout<<"Clown idNumber = "<<idNum<<", name = "<<clownName<<endl;

	};

private:
	string clownName;
	int idNum;

};

//functions
void printVectorContents(vector<Clown> &);
void printVectorContents2(vector<Clown> &);
void askWhoToPop(vector<Clown> &);
void popClown(vector<Clown> &, int);
void popThisClown(vector<Clown> &car, int popWho);

int IdNumbers ::counter = 0;

int main(int argc, const char * argv[])
{
	IdNumbers theCounter;

	vector<Clown> clowncar;     //sets up a vector for fish
	int vectorSize = 0;
	vectorSize = clowncar.size(); //get array size
	cout<<"**************"<<endl;
	cout<<"vector is size: "<<vectorSize<<endl;
	Clown pennywise("Pennywise", theCounter);
	clowncar.push_back(pennywise);
	Clown jwgacy("John Wayne Gacy", theCounter);
	clowncar.push_back(jwgacy);
	vectorSize = clowncar.size(); //get array size
	cout<<"**************"<<endl;
	cout<<"vector is size: "<<vectorSize<<endl;
	//printVectorContents(clowncar);
	printVectorContents2(clowncar);

	Clown bozo("Bozo", theCounter);
	clowncar.push_back(bozo);
	Clown krusty("Krusty", theCounter);
	clowncar.push_back(krusty);
	vectorSize = clowncar.size(); //get array size
	cout<<"**************"<<endl;
	cout<<"vector is size: "<<vectorSize<<endl;
	//printVectorContents(clowncar);
	printVectorContents2(clowncar);

	Clown ronald("Ronald McDonald", theCounter);
	clowncar.push_back(ronald);
	vectorSize = clowncar.size(); //get array size
	cout<<"**************"<<endl;
	cout<<"vector is size: "<<vectorSize<<endl;
	//printVectorContents(clowncar);

	//list clowns with ID numbers
	printVectorContents2(clowncar);
	//ask for an ID number of a clown to evict
	askWhoToPop(clowncar);


	cout<<"**********"<<endl;
	cout<<"vector is size: "<<vectorSize<<endl;
	printVectorContents2(clowncar);


	/*                                  This is an output section from a previous project
	 clowncar.pop_back();
	 vectorSize = clowncar.size(); //get array size
	 cout<<"**************"<<endl;
	 cout<<"vector is size: "<<vectorSize<<endl;
	 //printVectorContents(clowncar);
	 printVectorContents2(clowncar);

	 while(clowncar.size() > 0){
	 clowncar.pop_back();
	 }
	 vectorSize = clowncar.size(); //get array size
	 cout<<"**************"<<endl;
	 cout<<"vector is size: "<<vectorSize<<endl;
	 //printVectorContents(clowncar);
	 */


	return 0;
}


//Non-Member functions
void printVectorContents(vector<Clown> &car){
	for(int i=0; i < car.size(); i++){
		car[i].printClown();

	}
}

void printVectorContents2(vector<Clown> &car){
	for(int i=0; i < car.size(); i++){
		car[i].printClown2();
	}
}

void askWhoToPop(vector<Clown> &car){
	int popWho;
	cout<<"Please enter an ID number of a clown to kick out of the car: ";
	cin>>popWho;
	popThisClown(car, popWho);

	cout<<endl;

}

void popThisClown(vector<Clown> &car, int popWho){   //how to selectively delete from a vector????
	for (int j = 0; j < car.size(); j++)
	{
		cout<<"is "<<j<<" the right guy to bump?"<<endl;
		if(car[j].getClownId() == popWho)
		{
			cout<<"yeah, I'm here lookin' at erasing clown "<<j<<endl;
			//car.erase(j);   //tried this - didn't work
			car.erase (car.begin()+j);
		}
	}// end for
};



Was This Post Helpful? 1
  • +
  • -

#3 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6110
  • View blog
  • Posts: 23,670
  • Joined: 23-August 08

Re: problem iterating through and erasing vector objects

Posted 12 May 2014 - 09:17 AM

As if C++ wasn't scary enough already, you have to go and drag friggin' *clowns* into it???

Shudder.
Was This Post Helpful? 1
  • +
  • -

#4 jack-o  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 12-May 14

Re: problem iterating through and erasing vector objects

Posted 12 May 2014 - 09:29 AM

[quote name='CTphpnwb' date='12 May 2014 - 09:11 AM' timestamp='1399911086' post='2009822']
You're working on copies of the vector. You need to pass by reference so that you only work on the original.

Thanks, CT. That makes a lot of sense. However, I sometimes get tripped up with references (the idea is solid, making it work is the problem). I changed every passage of the vector into a reference, but that still leaves me confused with how to deal with this function:

void popThisClown(vector<Clown> *car, int popWho){   //how to selectively delete from a vector????
    for (int j = 0; j < car->size(); j++)
    {
        cout<<"is "<<j<<" the right guy to bump?"<<endl;
        if(car[j].getClownId() == popWho)                     <-----------------------------my error is here. It says there is no member called 'getClownID' ...
        {
            cout<<"yeah, I'm here lookin' at erasing clown "<<j<<endl;
            //car.erase(j);   //tried this - didn't work
            car->erase (car->begin()+j);
        }
    }// end for
};



thoughts?
(ps-everything else looks OK, but I haven't been able to verify without this working)
thanks again!

View PostJackOfAllTrades, on 12 May 2014 - 09:17 AM, said:

As if C++ wasn't scary enough already, you have to go and drag friggin' *clowns* into it???

Shudder.


---funny. My wife doesn't understand why it is that anyone should be afraid of clowns. Then again, she's never read, "it."
Was This Post Helpful? 0
  • +
  • -

#5 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3103
  • View blog
  • Posts: 10,898
  • Joined: 08-August 08

Re: problem iterating through and erasing vector objects

Posted 12 May 2014 - 09:51 AM

Pointers aren't identical to references. In C++ you should use references unless you truly need a pointer. Look at the code I posted. The only change I made from yours was to use references.
Was This Post Helpful? 1
  • +
  • -

#6 jack-o  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 12-May 14

Re: problem iterating through and erasing vector objects

Posted 12 May 2014 - 11:24 AM

View PostCTphpnwb, on 12 May 2014 - 09:51 AM, said:

Pointers aren't identical to references. In C++ you should use references unless you truly need a pointer. Look at the code I posted. The only change I made from yours was to use references.


Thank you again, CT.
I see that it is all working correctly now. I'll have to rewrite a couple of similar problems in order to fully digest the difference.
Best,
Jack
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1