Hello, I have a question regarding the first 'else if' statement. I'm trying to remove a string from 'library' but I get an error when it comes to the iterator. I'm barely learning about them and vectors. It gives me the error "no match for 'operator='. I originally had that part set to
cin>>game;
library.empty(game);
but I got an error saying 'no matcching function for call to'
Can someone help me please.
#include<iostream>
#include<vector>
#include<algorithm>
#include<string>
usingnamespace std;
int main()
{
char choice;
string gameTitle;
while (choice !='4')
{
cout<<"----What would you like to do?-----\n";
cout<<"(1)Add a new game to your library?\n";
cout<<"(2)Remove a game from your library?\n";
cout<<"(3)View your game library";
cout<<"(4)Quit the program?\n";
cin>>choice;
vector<string> library;
if(choice=='1')
{
//add game
cout<<"Please enter the game title. ";
cin>>gameTitle;
library.push_back(gameTitle);
}
elseif(choice=='2')
{
//remove game
cout<<"Please select which game to remove from your library.";
for (int i=0;i<library.size();i++)
{
cout<<"("<<i<<") "<<library[i];
}
int game;
cin>>game;
vector<int>::iterator myIterator;
myIterator = library[game];
*myIterator=library.empty();
}
elseif(choice=='3')
{
//view library
for (int i=0;i<library.size();i++)
{
cout<<library[i];
}
}
else
{
cout<<"Please enter a valid option.";
}
}
cout<<"Thank you for using this program.";
return 0;
}
library.empty() does not modify the vector. It just tells you information about it.
empty() will return true if the vector is empty (has no elements), or it will return false if it has at least 1 element.
If you want to remove a specific element from the vector, you want erase():
1 2 3 4 5 6 7 8
vector<int>::iterator myIterator = library.begin(); // start with the first iterator (begin)
myIterator += game; // add 'game' to it to advance it to the 'game'th element in the vector.
// At this point, 'myIterator' now points to the element you want to remove.
library.erase( myIterator ); // erase the element that our iterator points to
// ... or ... more simply:
library.erase( library.begin() + game );