It is impossible in C++ since you have to use iterator to erase.

Here's what I have:

main.ccp

#include <iostream>
#include <vector>
#include "Mastermind.h"

int main()
{
	vector<int>* answer=new vector<int>(4);
	cout<<"enter an code\n";
	for(int i=0; i<4; i++)
	{
		int k;
		cin>>k;
		answer->push_back(k);
	}
	//initalize population
	Mastermind a(5);
	//set answer
	a.setanswer(answer);
	a.showpopulation();
	a.evaluateblackandwhite();
	
	return 0;
}

Mastermind.h

#include <vector>
#include <string>

using namespace std;
class Mastermind
{
public:
	Mastermind(int populationsize);

	void showpopulation();

	void setanswer(vector<int>* answer);

	void evaluateblackandwhite();

	void calculatepopulationfitness();
	
	int evaluatepopulation();

	int	sum(int index);

	int randomizenumber();

private:
	int populationsize_;

	vector<int>* answer_;

	vector<vector<int>>* tempanswer_;

	vector<int>* fitness_;

	vector<vector<int>>* population_;

	vector<int>* totalnumberofpegs_;

	int* totalnumberofblackpegs_;

	int* totalnumberofwhitepegs_;

	//iterator through temp array


	//iterator through totalnumberofblackpegs
	vector<int>::iterator blackpegs;

	//iterator through totalnumberofwhitepegs
	vector<int>::iterator whitepegs;

	//iterate through population
	vector<vector<int>>::iterator pop;

	//iterate through individual
	vector<int>::iterator individual;

	vector<vector<string>>* feedback_;
};

/*inline bool operator ==(vector<string> a, string b)
{
	if(a==b)
	{
		return true;
	}
	else
	{
		return false;
	}
}*/

Mastermind.cpp

#include "Mastermind.h"
#include <iostream>

Mastermind::Mastermind(int populationsize)
{
	populationsize_=populationsize;
	population_=new vector<vector<int>>(populationsize_,NULL);
	for(int i=0; i<populationsize_; i++)
	{
		for(int j=0; j<4; j++)
		{
			population_->at(i).push_back(randomizenumber());
		}
	}
	answer_=new vector<int>(4);
	fitness_=new vector<int>(populationsize_);
	totalnumberofpegs_= new vector<int>(populationsize_);
	totalnumberofblackpegs_=new int[populationsize_];
	totalnumberofwhitepegs_=new int[populationsize_];
}

void Mastermind::showpopulation()
{
	for(pop=population_->begin(); pop!=population_->end(); ++pop)
	{
		for(individual=(*pop).begin(); individual!=(*pop).end(); ++individual)
		{
			cout<<*individual<<" ";
		}
		cout<<endl;
	}
}

void Mastermind::setanswer(vector<int>* answer)
{
	answer_=answer;
}

void Mastermind::evaluateblackandwhite()
{
	//copy answer to temp vector
	tempanswer_=new vector<vector<int>>(populationsize_,NULL);
	for(int i=0; i<populationsize_; i++)
	{
		for(int j=0; j<4; j++)
		{
			tempanswer_->at(i).push_back(answer_->at(j));
		}
	}

	//create feedback array
	feedback_=new vector<vector<string>>(populationsize_,NULL);
	for(int i=0; i<populationsize_; i++)
	{
		for(int j=0; j<4; j++)
		{
			feedback_->at(i).push_back("");
		}
	}

	for(pop=population_->begin(); pop!=population_->end(); pop++)
	{
		//check for black
		for(individual=answer_->begin(); individual!=answer_->end(); individual++)
		{
			if(answer_[individual]==population_[pop][individual])
			{
				feedback_[pop][individual].push_back("Match");
				for(blackpegs=tempanswer_[pop]->begin(); blackpegs!=tempanswer_[pop]->end(); blackpegs++)
				{
					//find first instance
					if(tempanswer_[pop][blackpegs]==answer_[individual])
					{
						tempanswer_[pop].erase(blackpegs);
						//erase data at that position
						break;
					}
				}
			}
		}
		//check for white(now go back and 
		//check for tempanswer but in wrong place
		/*for(int l=population_->at(i).front(); l<population_->at(i).back(); l++)
		{
			if(feedback_->at(i).at(l)=="")
			{
				for(int m=0; m<tempanswer_[i].size(); m++)
				{
					if(population_[i][l]==tempanswer_[i][m])
					{
						feedback_[i][l].push_back("Present");
						tempanswer_[i].erase(tempanswer_[i].begin()+m);
						break;
					}
				}
			}
		}
		
		//tally up feedback
		for(int a=0; a<feedback_[i].size(); a++)
		{
			if(feedback_[i][a]=="Match")
			{
				totalnumberofblackpegs_[i]++;
			}
			else if(feedback_[i][a]=="Present")
			{
				totalnumberofwhitepegs_[i]++;
			}
			else
			{
				totalnumberofwhitepegs_[i]=totalnumberofwhitepegs_[i];
				totalnumberofblackpegs_[i]=totalnumberofblackpegs_[i];
			}
		}*/
		
	}
	/*std::cout<<"after\n";
	for(int p=0; p<populationsize_; p++)
	{
		cout<<totalnumberofblackpegs_[p];
	}*/
}

/*void Mastermind::calculatepopulationfitness()
{
	//found fitness function online
	for(int k=0; k<populationsize_; k++)
	{
		fitness_[k]=((2*(totalnumberofblackpegs_[k]))+totalnumberofwhitepegs_[k])+sum(k);
	}
	for(int k=0; k<populationsize_; k++)
	{
		std::cout<<fitness_[k]<<std::endl;
	}
}

int Mastermind::evaluatepopulation()
{
	int mxm =(int)fitness_[0];
	for (int i=0; i<populationsize_; i++) 
	{
		if (fitness_[i]>mxm) 
		{
			mxm = fitness_[i];
		}
	}
	return mxm;
}

int Mastermind::sum(int index)
{
	int sum=0;
	for(int i=1; i<totalnumberofpegs_[index]-1; i++)
	{
		sum=sum+i;
	}
	return sum;
}*/

int Mastermind::randomizenumber()
{
	int d=rand()% 6;
	return d;
}

Thanks.

so... i decided to use object oriented programming approach seemed like it would be easier

Here's what I have so far incase any one has any ideas.

main.cpp

#include <iostream>

#include <stdio.h>
#include "population.h"
#include "Evolutionary Algorithm.h"


using namespace std;

int main()
{
	srand(NULL);

	//store code
	int answer[4];

	//store populationsize
	int populationsize;
	cout<<"please enter code\n";
	for(int i=0; i<4; i++)
	{
		cin>>answer[i];
	}
	cout<<"please enter a populationsize\n";
	cin>>populationsize;

	EvolutionaryAlgorithm b(populationsize);

	b.setanswer_(answer);

	//set population and print it out
	population a(populationsize);
	a.setpopulation();
	a.print();
	cout<<"total number of black\n";
	b.findpopulationblack(a);
	b.getblack();
	return 0;
}

position.h

#ifndef POSITION_H_INCLUDED
#define POSITION_H_INCLUDED

//#include <stdio.h>

class position
{
public:
	inline position()
	{
		position_=0;
	}

	inline void setposition(int pos)
	{
		position_=pos;
	}

	inline int getposition()
	{
		return position_;
	}

	inline int setranddata()
	{
		randdata_=rand()%6;
		return randdata_;
	}

	inline int getranddata()
	{
		return randdata_;
	}

private:
	int position_;

	int randdata_;
};

#endif;

individual.h

#ifndef INDIVIDUAL_H_INCLUDED
#define INDIVIDUAL_H_INCLUDED

#include "position.h"
#include <iostream>

using namespace std;

class individual
{
public:
	inline void setfirstposition(position one)
	{
		firstposition=one;
	}

	inline void setsecondposition(position two)
	{
		secondposition=two;
	}

	inline void setthirdposition(position three)
	{
		thirdposition=three;
	}

	inline void setfourthposition(position four)
	{
		fourthposition=four;
	}

	inline position getfirstposition()
	{
		return firstposition;
	}

	inline position getsecondposition()
	{
		return secondposition;
	}

	inline position getthirdposition()
	{
		return thirdposition;
	}

	inline position getfourthposition()
	{
		return fourthposition;
	}

	inline void setindivualposition()
	{
		firstposition.setposition(1);
		secondposition.setposition(2);
		thirdposition.setposition(3);
		fourthposition.setposition(4);
	}

	inline void setrandvalueforindivualposition()
	{
		firstposition.setranddata();
		secondposition.setranddata();
		thirdposition.setranddata();
		fourthposition.setranddata();
	}



private:
	//set positions
	position firstposition;
	position secondposition;
	position thirdposition;
	position fourthposition;

};


	ostream& operator <<(ostream& out, individual b)
	{
		//print out positions
		out<<b.getfirstposition().getposition();
		out<<b.getsecondposition().getposition();
		out<<b.getthirdposition().getposition();
		out<<b.getfourthposition().getposition();
		out<<std::endl;

		//print out random numbers stored at those positions
		out<<b.getfirstposition().getranddata();
		out<<b.getsecondposition().getranddata();
		out<<b.getthirdposition().getranddata();
		out<<b.getfourthposition().getranddata()<<std::endl;
		return out;
	}
#endif;
#ifndef POPULATION_H_INCLUDED
#define POPULATION_H_INCLUDED

#include "individual.h"

class population
{
public:

	inline population(int populationsize)
	{
		populationsize_=populationsize;
		indivual=new individual[populationsize_];
	}

	inline void setpopulation()
	{
		for(int j=0; j<populationsize_; j++)
		{
			indivual[j].setindivualposition();
			indivual[j].setrandvalueforindivualposition();
		}
	}

	inline void print()
	{
		for(int i=0; i<populationsize_; i++)
		{
			cout<<indivual[i];		
		}
	}

	inline individual returnindivual(int i)
	{
		return indivual[i];
	}

private:
	individual* indivual;
	
	int populationsize_;
};
#endif;

EvolutionarAlgorithm.h

#ifndef EVOLUTIONARYALGORITHM_H_INCLUDED
#define EVOLUTIONARYALGORITHM_H_INCLUDED
#include <iostream>
#include "population.h"

class EvolutionaryAlgorithm
{
public:
	inline EvolutionaryAlgorithm(int pop)
	{
		populationsize_=pop;
		blackpegs=new int[populationsize_];
		whitepegs=new int[populationsize_];
		answer_=new int[4];
		for(int k=0; k<populationsize_; k++)
		{
			blackpegs[k]=0;
		}
	}

	inline void setanswer_(int* answer)
	{
		answer_=answer;
	}

	inline void getblack()
	{
		for(int i=0; i<populationsize_; i++)
		{
			std::cout<<blackpegs[i]<<std::endl;
		}
	}

	inline void findpopulationblack(population a)
	{
		//go through population
		for(int i=0; i<populationsize_; i++)
		{
			//std::cout<<"enter first loop\n";
			//go through answer
			for(int j=0; j<4; j++)
			{
				//correct position and color
				if((a.returnindivual(i).getfirstposition().getposition()==j+1)&&(a.returnindivual(i).getfirstposition().getranddata()==answer_[j]))
				{
					blackpegs[i]++;
				}
				else if((a.returnindivual(i).getsecondposition().getposition()==j+1)&&(a.returnindivual(i).getsecondposition().getranddata()==answer_[j]))
				{
						blackpegs[i]++;
				}
				else if((a.returnindivual(i).getthirdposition().getposition()==j+1)&&(a.returnindivual(i).getthirdposition().getranddata()==answer_[j]))
				{
					blackpegs[i]++;
				}
				else if((a.returnindivual(i).getfourthposition().getposition()==j+1)&&(a.returnindivual(i).getfourthposition().getranddata()==answer_[j]))
				{
					blackpegs[i]++;
				}
				else
				{
					blackpegs[i]=blackpegs[i];
				}
			}
		}
	}

private:
	int populationsize_;

	int* blackpegs;

	int* whitepegs;

	int* answer_;

};

#endif;

so the problem was with the initialization of the arrays.
Here the answer
main.cpp

#include <iostream>
#include <ctime>
#include <stdio.h>
#include "Mastermind.h"

using namespace std;
 
int main()
{
	int* answer=new int[4];
	int populationsize=0;
	srand((unsigned)time(NULL));
	cout<<"please select 4 numbers out of the following options";
	cout<<"\nred:0\n"
		  "black:1\n"
		  "yellow:2\n"
		  "white:3\n"
		  "blue:4\n"
		  "green:5\n";
	for(int i=0; i<4; i++)
	{
		cin>>answer[i];
	}
	Mastermind a;
	a.setanswer(answer);
	cout<<"please enter a population size:\n";
	cin>>populationsize;
	a.setpopulationsize(populationsize);
	a.initializepopulation();
	a.calculatenumberofblackpegs();
	a.calculatenumberofwhitepegs();
	a.print();
	return 0;

}

mastermind.h

#include <iostream>
#include <cstdlib>

 
class Mastermind
{
public:
	Mastermind();
 
	void initializepopulation();
	void setpopulationsize(int populationsize);
	void setanswer(int* answer);
	void setgenerationnumber(int generationnumber);
	int randomizenumber();
 
	void calculatenumberofblackpegs();
	void calculatenumberofwhitepegs();
 
	void mutate();
	void crossover();
 
	void calculatepopulationfitness();
 
	int evaluatepopulation();
 
	void print();
 
private:
	int** population_;
	int populationsize_;
	int* answer_;
	int generationnumber_;
	int* totalnumberofblackpegs_;
	int* totalnumberofwhitepegs_;
	int* totalnumberofpegs_;
	int* fitness_;
	int sum(int index);
	int pickrandomposition();
	int** guess_pos;
	int** code_pos;
};

mastermind.cpp

#include "Mastermind.h"

 
Mastermind::Mastermind()
{
	populationsize_=0;
	answer_=new int[4];
	generationnumber_=0;
}
 
void Mastermind::initializepopulation()
{
	population_=new int*[populationsize_];
	for(int i=0; i<populationsize_; ++i)
	{
		population_[i]=new int[4];
	}
	for(int i=0; i<populationsize_; i++)
	{
		for(int j=0; j<4; j++)
		{
			population_[i][j]=randomizenumber();
		}
	}
	totalnumberofblackpegs_=new int[populationsize_];
	totalnumberofwhitepegs_=new int[populationsize_];
	totalnumberofpegs_=new int[populationsize_];
	fitness_=new int[populationsize_];
	guess_pos=new int*[populationsize_];
	for(int i=0; i<populationsize_; i++)
	{
		guess_pos[i]=new int[4];
	}
	code_pos=new int*[populationsize_];
	for(int m=0; m<populationsize_; ++m)
	{
		code_pos[m]=new int[4];
	}
	//initalize arrays
	for(int i=0; i<populationsize_; i++)
	{
		totalnumberofblackpegs_[i]=0;
		totalnumberofwhitepegs_[i]=0;
		totalnumberofpegs_[i]=0;
		for(int p=0; p<4; p++)
		{
			guess_pos[i][p]=3;
			code_pos[i][p]=3;
		} 
	}
}
 
void Mastermind::setpopulationsize(int populationsize)
{
	populationsize_=populationsize;
}
 
void Mastermind::setanswer(int* answer)
{
	answer_=answer;
}
 
void Mastermind::setgenerationnumber(int generationnumber)
{
	generationnumber_=generationnumber;
}
 
int Mastermind::randomizenumber()
{
	int d=rand()% 6;
	return d;
}

void Mastermind::calculatenumberofblackpegs()
{
	//intialize arrays
	for(int i=0; i<populationsize_; i++)
	{
		for(int j=0; j<4; j++)
		{
			if(answer_[j]==population_[i][j])
			{
				totalnumberofblackpegs_[i]++;
				guess_pos[i][j]=1;
				code_pos[i][j]=1;
			}
			else
			{
				totalnumberofblackpegs_[i]=totalnumberofblackpegs_[i];
			}
		}
	}
}

void Mastermind::calculatenumberofwhitepegs()
{
	for(int k=0; k<populationsize_; k++)
	{
		for (int  i=0; i <4; i++)
        {
            for (int j=0; j <4; j++)
            {
                if (population_[k][i] == answer_[j]           &&
                    code_pos[k][j] == 3  &&
                    guess_pos[k][i] == 3)
				{
						guess_pos[k][i]=2;
						code_pos[k][j]=2;
						totalnumberofwhitepegs_[k]++;
				}
				else
				{
					totalnumberofwhitepegs_[k]=totalnumberofwhitepegs_[k];
				}
            }
        }
	}
}
 
void Mastermind::print()
{
	for(int i=0; i<populationsize_; i++)
	{
		std::cout<<totalnumberofpegs_[i];
		std::cout<<"|";
		//print individual
		for(int j=0; j<4; j++)
		{
			std::cout<<population_[i][j];
		}
		std::cout<<"|";
		for(int k=0; k<4; k++)
		{
			std::cout<<guess_pos[i][k];
		}
		std::cout<<"|";
		for(int m=0; m<4; m++)
		{
			std::cout<<code_pos[i][m];
		}
		std::cout<<"|";
		std::cout<<totalnumberofblackpegs_[i];
		std::cout<<"|";
		std::cout<<totalnumberofwhitepegs_[i];
		std::cout<<"|";
		std::cout<<std::endl;
	}
}

/*void Mastermind::mutate()
{
 
}
 
void Mastermind::crossover()
{
 
}
 
void Mastermind::calculatepopulationfitness()
{
	//found fitness function online
	for(int k=0; k<populationsize_; k++)
	{
		fitness_[k]=((2*totalnumberofblackpegs_[k])+totalnumberofwhitepegs_[k])+sum(k);
	}
	for(int k=0; k<populationsize_; k++)
	{
		std::cout<<fitness_[k]<<std::endl;
	}
}
 
int Mastermind::evaluatepopulation()
{
	int mxm = fitness_[0];
	for (int i=0; i<populationsize_; i++) 
	{
		if (fitness_[i]>mxm) 
		{
			mxm = fitness_[i];
		}
	}
	return mxm;
}
 
int Mastermind::sum(int index)
{
	int sum=0;
	for(int i=1; i<totalnumberofpegs_[index]-1; i++)
	{
		sum=sum+i;
	}
	return sum;
}
 
int Mastermind::pickrandomposition()
{
	int d=rand()% 3;
	return d;
}*/
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.