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.

Dani AI

Generated

The "gibberish" is classic undefined behavior from buffer overruns and pointer misuse. already fixed it in some form, but for a robust, maintainable solution the changes fall into three clear areas: allocate the right buffer size, stop casting pointers to chars, and stop mixing allocation schemes (malloc/new and delete). Reading memory out of bounds or printing uninitialized bytes is what produces the random characters.

Prefer using standard containers (simpler and safer). Replace the manual char** population with std::vector<std::string> and generate each individual as a std::string. Example pattern:

std::vector<std::string> population(popSize);
for (int i = 0; i < popSize; ++i) {
    population[i] = generateRandomString(length); // returns std::string
    std::cout << population[i] << '\n';
}

If you must keep C-style arrays, fix these points: allocate length + 1 bytes for the terminating NUL, write exactly length characters (use i < length), set the terminator as '\0', copy returned strings (do not cast a char* to char), and free memory with the matching routine (free for malloc, delete[] for new[]). Also print with population_[j][k] (or correct pointer arithmetic), not the mixed expression that indexes the wrong memory.

Use tools to catch problems quickly: run under AddressSanitizer or Valgrind to find overruns and invalid frees. Prefer modern C++ types to avoid manual memory errors; see std::string and std::vector for guidance (std::string, std::vector). For correct allocation/deallocation rules see dynamic allocation.

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.