Why my code is working on everything except one instance?

56 Views Asked by At

I do homework for my lab on c++, I needed to get a number from keyboard and delete an element after inputted number in STL list. For some reason if I get 7 elements into the list, exactly {1,2,2,3,4,2,5} and ellement is 2 it outputs {1,2,2,2} and not {1,2,2,4,2}. Why?

#include <iostream>
#include <iterator>
#include <list>
using namespace std;
list<int> STL_MyList(std::list<int> g, int n);
int main()
{
    int s, d;
    list<int>::iterator it;
    cout<<"Enter how many elements you want in the list?"<<endl;
    cin>>s;
    cout<<"Start now: "<<endl;
    cin>>d;
    list<int> list1{d};
    for (int i = 0; i < s-1; ++i) {
        cin>>d;
        list1.push_back(d);
    }
    for (auto i : list1) {
        cout << i << ' ';
    }
    cout<<endl<<"After what number do you want to delete element?"<<endl;
    cin>>s;
    list1.operator=(STL_MyList(list1, s));
    cout<<"Result:"<<endl;
    for (auto i : list1) {
        cout << i << ' ';
    }
    cout<<endl<<"Do you want to do it again? (Yes - 1 / No - 0)"<<endl;
    cin>>s;
    if (s){
        main();
    } else{
        cout<<"Finish.";
    }

    return 0;
}

//Delete element after n in STL list
list<int> STL_MyList(list<int> g, int n){
    auto itr = g.begin();
    int a=n-1;
    for (itr = g.begin(); itr != g.end(); ++itr){
        if (*itr!=n && a==n)
        {
            itr=g.erase(itr);
        }
        a=*itr;
    }
    return g;
}

I tried changing the code and logic behind it - but it all resulted in a lot of mistakes and whole code going south.

1

There are 1 best solutions below

2
Orkhan Mirzoev On

So... I have figured it out through debugging it step by step The problem was that iterator went past the end() of a list doing it twice. I have fixed a problem and my code works correctly now, I simply needed to add break; so it won't go out of bounds of said list. Also made it into while loop, and renamed a function to represent what it was doing. Thank you everyone for help.

#include <iostream>
#include <list>
using namespace std;
list<int> DeleteElementAfterValue(std::list<int> g, int n);
int main()
{
    int s=1, d;
    while (s){
    list<int>::iterator it;
    cout<<"Enter how many elements you want in the list?"<<endl;
    cin>>s;
    cout<<"Start now: "<<endl;
    cin>>d;
    list<int> list1{d};
    for (int i = 0; i < s-1; ++i) {
        cin>>d;
        list1.push_back(d);
    }
    for (auto i : list1) {
        cout << i << ' ';
    }
    cout<<endl<<"After what number do you want to delete element?"<<endl;
    cin>>s;
    list1.operator=(DeleteElementAfterValue(list1, s));
    cout<<"Result:"<<endl;
    for (auto i : list1) {
        cout << i << ' ';
    }
    cout<<endl<<"Do you want to do it again? (Yes - 1 / No - 0)"<<endl;
    cin>>s;
    }
    cout<<"Finish.";
    return 0;
}

//Delete element after n in STL list
list<int> DeleteElementAfterValue(list<int> g, int n){
    auto itr = g.begin();
    int a=n-1;
    for (itr = g.begin(); itr != g.end(); ++itr){
        if (*itr!=n && a==n)
        {
            itr=g.erase(itr);
        }
        if (itr != g.end()) {
            a = *itr;
        }
        else{
            break;
        }
    }
    return g;
}