samedi 28 janvier 2023

c++ list is getting empty after adding into another list

Im writing a genetic algorithm in c++ as a first project for my programming course at uni. I had almost everything done but a problem appeared. When I'm reading my input file with first four individuals im trying to load every one into a list, and then I want to load it into a list of whole population but that's where the problem begins - my individual, which is properly written from file is being added into a population, but it appears as an empty list inside a population.

void read(ifstream& input, list<list<int>>& population) {
    string line; //line is a variable that saves an individual that is currently being added
    string chromosom;
    list<int> individual;
    while(getline(input, line)){
        individual.clear();
        chromosom = "";
        for (auto znak : line)     //loop that is getting rid out of space character 
        {
            if (int(znak) != int(' '))
            {
                chromosom += znak;
            }
            else
            {
                int liczba = atoi(chromosom.c_str()); 
                individual.push_back(liczba);
                chromosom = ""; 
            }
        }
        int liczba = atoi(chromosom.c_str());
        individual.push_back(liczba);                           
        population.push_back(individual);
    }

    input.close();
}

I ve tried to another ways to do that but every time i had the same results, I even tried to copy my colleagues code but still no change. Do you guys have any idea about why this error appears and how to solve it?

Aucun commentaire:

Enregistrer un commentaire