last element of vector duplicates after removing an element and function to check integer crashes

So I used the function to remove an element out of an vector and the last element duplicate. I understand that it needs to fulfill the set number of elements I declared. I tried to use pop_back function but the program crashes. When I remove the set number (15) I get an error with assigning the values of the vector.

1
2
3
4
5
6
7
8
9
10
11
12
13
vector<string> pg(15);
pg[1] = "Chris Paul PG"; pg[2] = "Mario Chambers PG"; pg[3] = "Mike Connely PG"; pg[4] = "Steve Nash PG"; pg[5] = "Russell Westbrook PG"; pg[6] = "Jeremy Lin PG"; pg[7] = "John Wall PG"; pg[8] = "Rajon Rondo PG";
    pg[9] = "Tony Parker PG"; pg[10] = "Deron Williams PG"; pg[11] = "Derrick Rose PG"; pg[12] = "Kyrie Irving PG"; pg[13] = "Brandon Jennings PG"; pg[14] = "Ty Lawson PG";
    //loop the output of the array
    for(int i=1; i<pg.size(); i++)
    {
        cout << a++ << ": " << pg[i] << endl;
    }
    cout << endl;
    cin >> pg_sel;
    user_sel.insert( user_sel.begin()+1, pg[pg_sel] ); //inserting the user input into another array which will hold the user picks
    pg.erase(pg.begin()+pg_sel); //removes the user's pick from the players' point guard array so player is not available anymore
    pg.pop_back();



Also I'm trying to create a function where it checks the user input making sure it's an integer. When I type an invalid input, I get the "invalid input" to appear and it allows the user to input another value. But when the user enters a correct value it crashes.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
int check_num(int check)
{
    do {
        if (!cin) {
            cin.clear();
            cout << "Invalid input. Please select a player by inputting the number that's next to the player's name" << endl;
            cin.ignore(numeric_limits<streamsize>::max(), '\n');
            cin >> check;
            continue;
        } else {
            break;
        }
    } while (true);
    
    return check;
}
Last edited on
anybody?
user_sel.insert( user_sel.begin()+1, pg[pg_sel] );
This will not work if user_sel is empty. If you want to add the element at the end of the vector you can use push_back.
user_sel.push_back(pg[pg_sel]);

I'm not sure why you use both erase and pop_back. That would remove 2 elements from the vector.

I don't think it is the check_num function that makes it crash.
user_sel.insert( user_sel.begin()+1, pg[pg_sel] );
This works, if I keep adding it to the end of the vector, it will place my 1st input in position 0. I want it to start at position 1. I have 4 more like this code and when i loop the output, it works.

The reason I'm trying to use both erase and pop_back is because I want the user to select from a list of players, whoever they pick will be place in a new vector holding their picks. Now I want the players list to remove the pick and shorten the vector -1, so the user can't pick that player anymore

and here is my function call for check_num. The pg_sel holds the user input or selection. I didn't want to send the pg_sel to the function because I need that variable for this user_sel.insert( user_sel.begin()+1, pg[pg_sel] );. So I created a new int variable check to hold the pg_sel value and send that to the function and then send the right integer back into the variable and set it to pg_sel.

1
2
3
4
5
6
7
8
9
cin >> pg_sel;
    check = pg_sel;
    check_num(check);
    pg_sel = check;
    check = 0;
    user_sel.insert( user_sel.begin()+1, pg[pg_sel] ); //inserting the user input into another array which will hold the user picks
    pg.erase(pg.begin()+pg_sel); //removes the user's pick from the players' point guard array so player is not available anymore
    //pg.pop_back();
    cout << endl;
I think it's better that you store the first element at index 0. It will make it easier for you because that is how it's supposed to be used. If you are not using index 0 you would still have to add an element to be stored at index 0 that you never use. If you still want the user to use numbers starting from 1 you can add 1 to the index when printing and subtract one from the number you read from the user.

user_sel.insert( user_sel.begin()+1, pg[pg_sel] );
Believe me. This will not work if user_sel is empty. You could of course add one element to user_sel so that it never is empty but if you do as I recommended and start using index 0 you don't get this problem and you can add an element to the start of the vector by doing
user_sel.insert( user_sel.begin(), pg[pg_sel] );

erase will decrease the size of the vector so no need to call extra functions to do that.

1
2
3
4
check = pg_sel;
check_num(check);
pg_sel = check;
check = 0;
You are not storing the return value of check_num in any variable, and I don't see why you need the extra variable check. What I think you are trying to do could be written in one line.
pg_sel = check_num(pg_sel);

Last edited on
ok. you are right, the fact that i wasn't using the index screwed everything over. Now everything works including the function. Thanks alot
Topic archived. No new replies allowed.