hello i am trying to write a program that allows user to make a list using vectors and iterators
i am able to add a title, list items, remove all, but were my trouble is when i want to remove just one from the list i am not sure how if someone could help me please

this is what i got problem is in the if ( input == remove)

#include <iostream>
#include <vector>
#include <string>


using namespace std;
bool done = false;
int main()
  {
    
    vector <string> vectorGameList;
	cout << "\t\t\tWelcome!\n\n";
    cout << "Enter 'quit' to end list & close.\n";
    cout << "Enter 'list' to lists games on list so far.\n"; 
	cout << "Enter 'add' to add games to list.\n";
	cout << "Enter 'remove' to remove a game from the list.\n";
	cout << "Enter 'clear' to remove all games on list.\n";
    

do 
{
    vectorGameList;
	
	vector< string >::iterator myIterator;
    vector< string >::const_iterator iter;
	 
     cout << "Enter your selection:  ";
	 string input;
	 cin >> input;
	
	 if ( input == "add" )
	 {
       
	  string gametoAdd;
      cout << "\nwhat game would you like to add:    ";
      cin >> gametoAdd;
	  vectorGameList.push_back(gametoAdd);
      done = false;
	 }
	
	if (input == "list")
    {
           cout << "\nYou have " << vectorGameList.size() << " game(s) on list so far" << endl;  
		   cout << "  Your Game List includes:" << endl;
            
            int gameNum= 1;
            for (iter = vectorGameList.begin(); iter != vectorGameList.end(); ++iter)
            {
            cout << gameNum++ << ") " << *iter << endl;
            }
       done = false;
     }
	 
	 if ( input == "quit" )
     {
        done = true;
     }
	
	
   
	if ( input == "remove" )// here my problem begins, if user inputs remove i then want them to input what item they want off the list
	{
     cout << " which do you want to remove    ";
	 // cannot remove certain string from vector
     vectorGameList.erase((vectorGameList.begin()); 
	 done = false;
    }
     
	if ( input == "clear" )
	{
	  vectorGameList.clear();
	  cout << "\nYOUR LIST IS NOW EMPTY!!!" << endl;
	  done = false;
    }


	
}while(!done);


cin.get();
return 0;
           
          
}

thanks alot

Edited 5 Years Ago by gl7: n/a

Below is a example of one way to remove an item from a vector using an iterator.

class Eraser
{
public:
    Eraser()
    {
        _vector.push_back("fred");
        _vector.push_back("alice");
        _vector.push_back("billy");
    }
    bool remove(const std::string &who_)
    {
        for( TVec::iterator i=_vector.begin(); i!=_vector.end(); ++i )
        {
            if( *i == who_ )
            {
                std::cout << who_ << " is removed" << std::endl;
                _vector.erase(i);
                return true;
            }
        }

        std::cout << who_ << " is not in list" << std::endl;
        return false;
    }
    void show()
    {
        for( size_t i=0; i < _vector.size(); i++ )
        {
            std::cout << _vector[i] << std::endl;
        }
    }
private:
    typedef std::vector<std::string> TVec;
    TVec _vector;
};

thanks for replys, jonsca, i know of that process to remove one, what i am trying to do is allow the user to input the list item they want to remove.

template<> im not really familiar with classes, kind of a newb to c++, so your process is not completely clear to me is there a process that would work using simpler format?

1. Iterate through all items in the vector: for loop below
2. find item in vector that matches your item to remove: *i == who_
3. remove item from vector using iterator: _vector.erase(i)

for( TVec::iterator i=_vector.begin(); i!=_vector.end(); ++i )
        {
            if( *i == who_ )
            {
                std::cout << who_ << " is removed" << std::endl;
                _vector.erase(i); return;
            }
        }

Edited 5 Years Ago by template<>: n/a

template i get your the logic of that process, but trouble writting it
first what is done by line:
TVec::iterator also
in line 4 it is meant to do if the string who_ is in our iter then it will remove right?
this is how tried after your help how am i messing up

if ( input == "remove" )
	{
     string who_;
     cout << " which do you want to remove    ";
	 cin >> who_;
     for( iter = vectorGameList.begin(); iter !=vectorGameList.end(); ++iter )
        {
            if( *iter == who_ )
            {
               cout << who_ << " is removed" << endl;
                vectorGameList.erase(who_);
            }
        }
      
	 done = false;
    }

typedef is handy when using stl containers to avoid typing. Read up on it. Below is a simple sample to help to compile, run, test and understand. Play around with it and when you understand whats going on, incorporate similar logic into your application.

Iterators are tricky and confusing at first, but its extensively used, so its worthwhile investment in time.

#include <iostream>
#include <vector>

int main( int argc, char *argv[])
{
    /// typedef lets us alias names
    typedef std::vector<std::string> TVec;

    /// below is a list of favarite dogs
    TVec v;
    v.push_back("jake");
    v.push_back("lucy");
    v.push_back("paws");

    std::cout << "Please enter name to remove:" << std::endl;
    std::string who;
    std::getline(std::cin,who);
 
    /// find and remove name on list
    bool found=false;
    for( TVec::iterator iter=v.begin(); iter!=v.end() && found==false; ++iter )
    {
        if( *iter == who )
        {
            v.erase(iter); // iterator passed to erase
            found=true; // exit the loop
        }
    }

    /// inform user of status
    if(found == true )
    {
        std::cout << who << " is removed" << std::endl;
    }
    else
    {
        std::cout << who << " is not on list" << std::endl;
    }

    return 0;
}

actually have more question following template<>; example i thing i almost have it down
but i am getting an error message that reads "instantiated from here" at line 25
when have v.erase(iter);

could some one explain to me why that is?

I am able to take the code out of above and compile without any problem.

g++ -g sample.cpp -o sample

Are you able to do that?

Just for the record, if you #include <algorithm>, you can replace lines 20 to 38 in template<>'s code with the following:

TVec::iterator iter = std::find(v.begin(), v.end(), who); //returns the iterator that matches 'who'.
  if(iter != v.end() ) {
    v.erase(iter);
    std::cout << who << " is removed" << std::endl;
  } else
    std::cout << who << " is not on list" << std::endl;

As for the compilation error. There shouldn't be one with either template<>'s code or the above. When you get an error that says "instantiated from here" this is not the error it is just a kind of back-tracking of the error. There must be a real error message before that message (this "instantiated from here" just tells you where the error comes from in your code, but the actual error is usually deeper down). The only source of error I can think of is if the type of "iter" is not correct (make sure that if "v" is of type std::vector<std::string> , then "iter" should be of type std::vector<std::string>::iterator (or equivalently with a typedef).

mike 2000 using algorithm so your code worked great thanks so much and template<>
thanks alot to
able to figure out

This question has already been answered. Start a new discussion instead.