-2

I tried my best to figure this out but it was unsuccessful :C

PROBLEM

for (Treniruote& t : treniruotes)
            {
                if (t.data == duotaData)
                {
                    if (t.laikas == duotasLaikas)
                    {
                        treniruotes.remove(t);
                    }
                }
            }

FULL CODE:

#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <sstream>
#include <list>
#include <ctime>

using namespace std;

struct Treniruote
{
    string data, laikas, vardas, pavarde, pratimai;
};

void NuskaitytiIsFailo(list<Treniruote>& treniruotes)
{
    string line, word;

    fstream file("planas.csv", ios::in);
    if (file.is_open())
    {
        while (getline(file, line))
        {
            stringstream str(line);

            Treniruote row;

            if (getline(str, word, ','))
                row.data = word;
            if (getline(str, word, ','))
                row.laikas = word;
            if (getline(str, word, ','))
                row.vardas = word;
            if (getline(str, word, ','))
                row.pavarde = word;
            if (getline(str, word, ','))
                row.pratimai = word;

            treniruotes.push_back(row);
        }
    }
    else
        cout << "Could not open the file";
}
void rastiKlientoInfo(list<Treniruote>& treniruotes)
{
    int a;
    string fname, pavarde;
    
    cout << "Write clients surname:";
    cin >> pavarde;

    cout << "Output to the screen press -1\n";
    cout << "Output to the file press - 2\n";
    cout << "Output all exercise needed to be done in a week - 3\n";
    cout << "Your choice:";
    cin >> a;

    if (a == 2)
    {
        cout << "Enter the file name: ";
        cin >> fname;
    }
    else if (a == 3)
    {
        cout << "Enter the file name: ";
        cin >> fname;
    }
    else
    {

    }


    for (Treniruote& t : treniruotes)
    {
        if (t.pavarde == pavarde)
        {
        
            if (a == 1)
            {
                cout << t.data << endl;
                cout << t.laikas << endl;
                cout << t.vardas << endl;
                cout << t.pavarde << endl;
                cout << t.pratimai << endl;
                cout << endl;
            }
            else if (a == 3)
            {
                fstream file(fname + ".csv", ios::app);
                file << t.pratimai << endl;
            }
            else 
            {
                fstream file(fname + ".csv", ios::app);
                file << t.data << endl;
                file << t.laikas << endl;
                file << t.vardas << endl;
                file << t.pavarde << endl;
                file << t.pratimai << endl;
                file << endl;
            }
            
        }
    }
}

void PakeistiReiksmes(list<Treniruote>& treniruotes)
{
    string duotasLaikas, duotaData;

    cout << "irasykite norima data ir laika ,kada norite pakeisti duomenys" << endl;
    cout << "data(formatas(XXXX.XX.XX)):";
    cin >>duotaData;
    cout << endl;
    cout << "laikas(formatas(XX:XX-XX:XX)):";
    cin >>duotasLaikas;


    for (Treniruote& t : treniruotes)
    {
        if (t.data == duotaData)
        {
            if (t.laikas == duotasLaikas)
            {
                t.vardas = t.pavarde = t.pratimai = "obolys";
            }
        }
    }
}

void SukurtiTuscius(list<Treniruote> &treniruotes)
{
    for (int i = 27; i <= 30; ++i)
    {
        stringstream diena;
        diena << "2022.11." << i;
        for (int j = 8; j <= 19; ++j)
        {
            stringstream valanda;
            valanda << j << ":00-"<< j+1 <<":00";

            Treniruote t;
            t.data = diena.str();
            t.laikas = valanda.str();
            t.vardas = t.pavarde = t.pratimai = "nera";

            treniruotes.push_back(t);
        }
    }
}

void ParodytiVisaTvarkarasti(list<Treniruote>& treniruotes)
{
    for (Treniruote& t : treniruotes)
    {
        cout << t.data << endl;
        cout << t.laikas << endl;
        cout << t.vardas << endl;
        cout << t.pavarde << endl;
        cout << t.pratimai << endl;
        cout << endl;
    }
}

void salinti_pridėti(list<Treniruote>& treniruotes)
{

    
    
}

void ProgramosPabaiga(list<Treniruote>& treniruotes)
{

    fstream file("planas.csv", ios::out);

    for (Treniruote& t : treniruotes)
    {
        file << t.data <<","<< t.laikas << "," << t.vardas << "," << t.pavarde << "," << t.pratimai;
        file << endl;
    }
    cout << "PROGRAMA BAIGTA";
    exit(0);
}

int main()
{
    int choice;

    bool gameOn = true;

    list<Treniruote> treniruotes;

    struct tm newtime;
    time_t now = time(0);
    localtime_s(&newtime,&now);

    auto month = to_string(1 + newtime.tm_mon);
    auto day = to_string(newtime.tm_mday);
    auto hour = to_string(newtime.tm_hour);
    auto hour_pridėta = to_string(1 + newtime.tm_hour);

    //sukuria .csv faila pagal irasyta pavadinima
    //string fname;
    //cout << "Enter the file name: ";
    //cin >> fname;
    // fstream file(fname + ".csv", ios::app);
    
    //Menu
    while (gameOn != false) {
        cout << "*******************************\n";
        cout << " 1 - Create empty weekly plan.\n";
        cout << " 2 - Show the full schedule.\n";
        cout << " 3 - Read the weekly work plan.\n";
        cout << " 4 - Change the schedule field information.\n";
        cout << " 5 - Find the given client's full weekly workouts\n";
        cout << " 7 - Exit.\n";
        cout << "*******************************\n";
        for (Treniruote& t : treniruotes)
        {
            if (t.data == "2022." + month + "." + day)
            {
                if (t.laikas == hour + ":00-" + hour_pridėta + ":00")
                {
                    cout << t.data << endl;
                    cout << t.laikas << endl;
                    cout << t.vardas << endl;
                    cout << t.pavarde << endl;
                    cout << t.pratimai << endl;
                    cout << endl;
                }
            }

        }
        cout << "*******************************\n";
        cout << " Enter your choice and press return: ";
        cin >> choice;

        switch (choice)
        {
        case 1:
            SukurtiTuscius(treniruotes);
            break;
        case 2:
            ParodytiVisaTvarkarasti(treniruotes);
            break;
        case 3:
            NuskaitytiIsFailo(treniruotes);
            break;
        case 4:
            PakeistiReiksmes(treniruotes);
            break;
        case 5:
            rastiKlientoInfo(treniruotes);
            break;
        case 6:
        {
            string duotasLaikas, duotaData;

            cout << "irasykite norima data ir laika ,kada norite pakeisti langeli" << endl;
            cout << "data(formatas(XXXX.XX.XX)):";
            cin >> duotaData;
            cout << endl;
            cout << "laikas(formatas(XX:XX-XX:XX)):";
            cin >> duotasLaikas;

            for (Treniruote& t : treniruotes)
            {
                if (t.data == duotaData)
                {
                    if (t.laikas == duotasLaikas)
                    {
                        treniruotes.remove(t);
                    }
                }
            }
        }
            break;
        case 7:
        {
            ofstream file;
            file.open("planas.csv", std::ofstream::out | std::ofstream::trunc);
            file.close();
            ProgramosPabaiga(treniruotes);
            break;
        }
        default:
            cout << "Not a Valid Choice. \n";
            cout << "Choose again.\n";
            cin >> choice;
            break;
        }
    }
    return 0;
}
  • 1
    Your "PROBLEM" description is a code dump. It says nothing about what the problem is. Also, don't post the full code. Post a [mre] where you've removed everything but the part you actually need help with. – Ted Lyngmo Nov 28 '22 at 22:22
  • 5
    Thou shalt not modify the list thou iterate. At least not with a range-based `for`. Removing the item breaks the book-keeping that's being hidden out of sight. – user4581301 Nov 28 '22 at 22:22
  • https://stackoverflow.com/questions/2088495/how-to-remove-all-even-integers-from-setint-in-c/2088542#2088542 – pm100 Nov 28 '22 at 22:24
  • Here's another [Can you remove elements from a std::list while iterating through it?](https://stackoverflow.com/questions/596162/can-you-remove-elements-from-a-stdlist-while-iterating-through-it) – user4581301 Nov 28 '22 at 22:25
  • TL;DR version: You can't do this with range-based-`for`. Range-for is fantastic for simple iteration, but it falls all over itself when you want to do anything complicated. You need to drop back to old-school iteration and update the iterator (or index) manually. – user4581301 Nov 28 '22 at 22:28

1 Answers1

2

You must not remove items from a list while iterating through a range-based for loop. Under the hood, the loop stores a begin and end iterator of the original list, and iterate all items between them, and attempting to remove items during the loop will make end unreachable.

Instead, you could simply use std::list::remove_if to remove all elements that satisfy a certain condition:

my_list.remove_if([a, b](const auto& element){
    return element.a == a && element.b == b;
};
Ranoiaetep
  • 5,872
  • 1
  • 14
  • 39