in a rut

I’m stuck. I’ve been trying to re-jigger my code to add a dialog asking for an ID number to delete from the vector and then doing it, but so far, I’ve gotten nowhere. Sure, I can create the dialog and get the info from the user, but I have not yet been able to use that info to selectively delete a clown from the car.

Recall, the purpose of this exercise is to ensure that I can handle a vector of objects, adding and deleting objects at will. A secondary objective is to be sure that I am assigning unique ID numbers to each object so they can be tracked accurately.

my function asking who to delete:

void askWhoToPop(vector<Clown> car){

    int popWho;

    cout<<“Please enter an ID number of a clown to kick out of the car: “;

    cin>>popWho;

    Clown::popThisClown(car, popWho);

    cout<<endl;

}

 

my function for deleting an object:

void popThisClown(vector<Clown> car, int popWho){   //how to selectively delete from a vector????

for (int j = 0; j < car.size(); j++)

        {

if(car[j].idNum == popWho)

            {

delete car[j];

                car.pop_back();

            }

        }// end for

    };

suggestions????

 

unable to add 1

Grrrr.

I know I have set this problem up oddly. However, the point is to handle objects correctly, not to be the cleanest code. i.e. I’m trying to do this in a kind of round-about way just to ensure that I understand how objects work – and even more importantly, how a vector of objects works. 

I feel like I am getting the hard part to work fine (creating objects on the fly, storing them in a vector and calling up details from the objects. However, I have stored a counter integer in a vector and I can’t seem to get it to work correctly. Why store a counter in an object? I want to have the counter in a safe place – away from the other objects, but not as a global variable. 

It looks like it is resetting to 0 each time I reach in to get a unique ID number.

Does anyone out there see what I’m doing wrong?

 

code:

//

//  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. Ronald McDonald gets kicked out (4)

 

 6. All the rest stop for a drink at the Alehouse together (0)

 

 */

 

#include <iostream>

#include <vector>

usingnamespacestd;

using std::vector;

 

class IdNumbers{     // a place to store a counter for creating idNumbers

public:

    IdNumbers(int x){

        absoluteNumber = x;

    }

    int getANewNumber(void){

        cout<<“number is “<<absoluteNumber<<endl;

        absoluteNumber = advanceNumber();

        cout<<“number is now “<<absoluteNumber<<endl;

        returnabsoluteNumber;

    }

    int advanceNumber(void){

        absoluteNumber ++;

        returnabsoluteNumber;

    }

private:

    int absoluteNumber;

};

 

class Clown{   // the clown objects

public:

    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;

        

    };

    

    

private:

    string clownName;

    int idNum;

    

};

 

//functions

void printVectorContents(vector<Clown>);

 

int main(int argc, const char * argv[])

{

    

    IdNumbers *absoluteNumbers = newIdNumbers(0);   //initializes a running id Number

    

    

    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”, *absoluteNumbers);

    clowncar.push_back(pennywise);

    Clown jwgacy(“John Wayne Gacy”, *absoluteNumbers);

    clowncar.push_back(jwgacy);

    vectorSize = clowncar.size(); //get array size

    cout<<“**************”<<endl;

    cout<<“vector is size: “<<vectorSize<<endl;

    printVectorContents(clowncar);

    

    Clown bozo(“Bozo”, *absoluteNumbers);

    clowncar.push_back(bozo);

    Clown krusty(“Krusty”, *absoluteNumbers);

    clowncar.push_back(krusty);

    vectorSize = clowncar.size(); //get array size

    cout<<“**************”<<endl;

    cout<<“vector is size: “<<vectorSize<<endl;

    printVectorContents(clowncar);

    

    Clown ronald(“Ronald McDonald”, *absoluteNumbers);

    clowncar.push_back(ronald);

    vectorSize = clowncar.size(); //get array size

    cout<<“**************”<<endl;

    cout<<“vector is size: “<<vectorSize<<endl;

    printVectorContents(clowncar);

    

    clowncar.pop_back();

    vectorSize = clowncar.size(); //get array size

    cout<<“**************”<<endl;

    cout<<“vector is size: “<<vectorSize<<endl;

    printVectorContents(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;

}

 

 

//functions

void printVectorContents(vector<Clown> car){

    for(int i=0; i < car.size(); i++){

        car[i].printClown();

        

    }

}

 

 

/*  output:

 

 **************

 vector is size: 0

 number is 0

 number is now 1

 number is 0

 number is now 1

 **************

 vector is size: 2

 idNumber = 1

 name = Pennywise

 idNumber = 1

 name = John Wayne Gacy

 number is 0

 number is now 1

 number is 0

 number is now 1

 **************

 vector is size: 4

 idNumber = 1

 name = Pennywise

 idNumber = 1

 name = John Wayne Gacy

 idNumber = 1

 name = Bozo

 idNumber = 1

 name = Krusty

 number is 0

 number is now 1

 **************

 vector is size: 5

 idNumber = 1

 name = Pennywise

 idNumber = 1

 name = John Wayne Gacy

 idNumber = 1

 name = Bozo

 idNumber = 1

 name = Krusty

 idNumber = 1

 name = Ronald McDonald

 **************

 vector is size: 4

 idNumber = 1

 name = Pennywise

 idNumber = 1

 name = John Wayne Gacy

 idNumber = 1

 name = Bozo

 idNumber = 1

 name = Krusty

 **************

 vector is size: 0

 Program ended with exit code: 0

 

*/