Help with deleting a certain vector

I would like the user to be able to delete a a player from the vector list based on the jersey number. I just don't know how to do it.

#include <iostream>
#include <vector>
using namespace std;

int main() {
int numPlayers = 5;
vector<int> playerRating(5);
vector<int> playerNumber(5);
bool keepGoing = true;
char userInput = 'a';
int newNumber = 0;
int newRating = 0;

for (int i = 0; i < numPlayers; i++) {
cout << "Enter player " << i + 1 << "'s jersey number: ";
cin >> playerNumber.at(i);
cout << endl;
cout << "Enter player " << i + 1 << "'s rating: ";
cin >> playerRating.at(i);
cout << endl << endl;
}

cout << endl << "ROSTER" << endl;

for (int i = 0; i < numPlayers; i++) {
cout << "Player " << i + 1 << " -- " << "Jersey number: ";
cout << playerNumber.at(i) << ", Rating: " << playerRating.at(i);
cout << endl;
}

cout << endl;
cout << "MENU" << endl;
cout << "a - Add player" << endl;
cout << "d - Remove player" << endl;
cout << "u - Update player rating" << endl;
cout << "r - Output players above a rating" << endl;
cout << "o - Output roster" << endl;
cout << "q - Quit" << endl << endl;
cout << "Choose an option: " << endl << endl;
cin >> userInput;

while (keepGoing == true) {
if (userInput == 'q') {
keepGoing = false;
break;
}
if (userInput == 'o') {
cout << "ROSTER" << endl;

for (int i = 0; i < numPlayers; i++) {
cout << "Player " << i + 1 << " -- " << "Jersey number: ";
cout << playerNumber.at(i) << ", Rating: " << playerRating.at(i);
cout << endl;
}
}
if (userInput == 'a') {
cout << "Enter another player's jersey number: ";
cin >> newNumber;
cout << endl;
playerNumber.push_back(newNumber);

cout << "Enter another player's rating: ";
cin >> newRating;
cout << endl;
playerRating.push_back(newRating);
numPlayers++;
}
if (userInput == 'd'){
cout << "Enter a jersey number: ";
cin >> newNumber;
playerNumber.pop_back();
}

cout << endl;
cout << "MENU" << endl;
cout << "a - Add player" << endl;
cout << "d - Remove player" << endl;
cout << "u - Update player rating" << endl;
cout << "r - Output players above a rating" << endl;
cout << "o - Output roster" << endl;
cout << "q - Quit" << endl << endl;
cout << "Choose an option: " << endl;
cin >> userInput;
}



system("pause");
return 0;
}
Go through the vector, one element at a time. You can do this with a for loop.

Compare each element with the value you want to remove.

When you find the right one, remove it. You can do this with erase http://www.cplusplus.com/reference/vector/vector/erase/
closed account (LA48b7Xj)
Here is a example that might help you

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

int main()
{
    vector<int> v {1,2,3,4,5};

    auto it = find(v.begin(), v.end(), 4);

    if(it != v.end())
        v.erase(it);

    for(auto e : v)
        cout << e << '\n';
}

Topic archived. No new replies allowed.