When i run main it prints out gibberish not sure why.

main.cpp

#include <iostream>
#include <string>
#include "phrase.h"
#include "EA.h"
#include <cstdlib>
#include <time.h>

int main()
{
	phrase a;
	EA b;

	std::string phrase;
	int populationsize;

	std::cout<<"enter a population size\n";
	std::cin>>populationsize;
	std::cin.ignore();
	b.setpopulationsize(populationsize);

	std::cout<<"enter a string\n";
	getline(std::cin,phrase);
	a.setphraselength(phrase);
	b.setlength(phrase);
	std::cout<<"the phrase length is"<<a.getphraselength()<<std::endl;

	srand ((unsigned)time(0));
	
	
	b.initalizepopulation();
	/*while(phrase!=bestofpopulation())
	{
		//mutate and crossover to create new population
	}*/
	
	return 0;
}

EA.h

#ifndef EA_H_INCLUDED
#define EA_H_INCLUDED
#include <string>
#include "phrase.h"
#include <iostream>

class EA:public phrase
{
public:
	EA();

	~EA();

	void setgenerationnumber(int generationnumber);
	int getgenerationnumber();

	void setpopulationsize(int populationsize);

	void setlength(std::string length);

	void initalizepopulation();

	//void mutate();

	//void crossover();

	std::string bestofpopulation();

private:
	int populationsize_;
	int generationnumber_;
	int length_;
	char** population_;
	
};
#endif // INTERFACE_H_INCLUDED

EA.cpp

#include "EA.h"

EA::EA()
{
	populationsize_=0;
	generationnumber_=0;
	length_=0;
	population_=NULL;
}

EA::~EA()
{
	delete population_;
}

void EA::setgenerationnumber(int generationnumber)
{
	generationnumber_=generationnumber;
}

int EA::getgenerationnumber()
{
	return generationnumber_;
}

void EA::setpopulationsize(int populationsize)
{
	populationsize_=populationsize;
}

void EA::setlength(std::string length)
{
	length_=length.length();
}

void EA::initalizepopulation()
{
	population_=(char**)(malloc(sizeof(char*)*populationsize_));
	for(int i=0; i<populationsize_; ++i)
	{
		*(population_+i)=(char*)(malloc(sizeof(char)*length_));
	}

	for(int j=0; j<populationsize_; ++j)
	{
		std::cout<<j<<std::endl;
		for(int k=0; k<length_; ++k)
		{
			population_[j][k]=(char)generatenewIndividual();
			std::cout<<*((*population_+k)+j);
		}
		std::cout<<"\n";
	}
}
	/*void mutate()

	void crossover()

	std::string bestofpopulation()
*/

phrase.h

#ifndef PHRASE_H_INCLUDED
#define PHRASE_H_INCLUDED

#include <string>

class phrase
{
public:	
	phrase();

	void setphraselength(std::string phraselength);
	int getphraselength();

	//randomly intialize a string
	char* generatenewIndividual();
private:
	char randomizecharacter();
	
private:
	int phraselength_;	
};
#endif // INTERFACE_H_INCLUDED

phrase.cpp

#include "phrase.h"
#include <string>

phrase::phrase()
{
	phraselength_=0;
}

void phrase::setphraselength(std::string phraselength)
{
	phraselength_=phraselength.length();
}

int phrase::getphraselength()
{
	return phraselength_;
}

char phrase::randomizecharacter()
{
	int n = rand() % 26;
    char c = (char)(n+65);
	return c;
}

char* phrase::generatenewIndividual()
{
	char* random = new char[phraselength_];
	for(int i=0; i<=phraselength_; ++i)
	{
		random[i]=randomizecharacter();
	}
	random[phraselength_]=NULL;
	return random;
}

thanks.

got it to work but not the way i wanted. oh well.

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.