So I'm trying to create a fairly simple program that allows a user to modify a list of games. The user can add, remove or simply call an enumeration of the games currently inside the list.
The compiler keeps telling me that there is a problem with my use of the find function. My feeling is that I am not allowed to use it with string vectors. Any ideas what th problem is and how to solve it?

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

int main()
{
    int choice;
    int location;
    string game;
    
    vector<string> games;
    vector<string>::const_iterator iter;
    
    cout << "1: List all games.";
    cout << "2: Add a new game.";
    cout << "3: remove a game.";
    cin >> choice;
    switch (choice)
    {
        case 1:
             cout << "what is the title of the game you would like to add?\n";
             cin >> game;
             games.push_back(game);
             break;
        case 2:
             cout << "what is the title of the game you would like to remove?\n";
             cin >> game;
             games.erase(games.find(game));
             break;
        case 3:
             for (iter = games.begin(); iter != games.end(); iter++)
             {
                 cout << *iter;
             }
                 
    }
return 0;
}

So I'm trying to create a fairly simple program that allows a user to modify a list of games. The user can add, remove or simply call an enumeration of the games currently inside the list.
The compiler keeps telling me that there is a problem with my use of the find function. My feeling is that I am not allowed to use it with string vectors. Any ideas what th problem is and how to solve it?

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

int main()
{
    int choice;
    int location;
    string game;
    
    vector<string> games;
    vector<string>::const_iterator iter;
    
    cout << "1: List all games.";
    cout << "2: Add a new game.";
    cout << "3: remove a game.";
    cin >> choice;
    switch (choice)
    {
        case 1:
             cout << "what is the title of the game you would like to add?\n";
             cin >> game;
             games.push_back(game);
             break;
        case 2:
             cout << "what is the title of the game you would like to remove?\n";
             cin >> game;
             games.erase(games.find(game));
             break;
        case 3:
             for (iter = games.begin(); iter != games.end(); iter++)
             {
                 cout << *iter;
             }
                 
    }
return 0;
}

I don't think it matters whether it is a vector of characters, integers, strings, doubles, whatever. vector does not have a find function. You may have to write your own.
http://www.cppreference.com/cppvector/index.html

I am currently reading Beginning C++ through game programming second edition. In one of the author's examples he uses the find function with a vector. Take a look at an excerpt of code. oo... I think I am understanding what he did here. Let me see if I can redo the code. Take a look anyways; I've bolded (unsuccessfully) the section of code where he uses find below.

#include <iostream>
#include <vector>
#include <algorithm>
#include <ctime>
#include <cstdlib>

using namespace std;

int main()
{
    vector<int>::const_iterator iter;

    cout << "Creating a list of scores.";
    vector<int> scores;
    scores.push_back(1500);
    scores.push_back(3500);
    scores.push_back(7500);

    cout << "\nHigh Scores:\n";
    for (iter = scores.begin(); iter != scores.end(); ++iter)
        cout << *iter << endl;
        
    cout << "\nFinding a score.";
    int score;
    cout << "\nEnter a score to find: ";
    cin >> score;
[B]iter = find(scores.begin(), scores.end(), score);[/B]
    if (iter != scores.end())
        cout << "Score found.\n";
    else
        cout << "Score not found.\n";

So I've figured one problem out only to encounter another. Apparently, in the following code, the line " if (find (games.begin(), games.end(), game) != string::npos)" is invalid. Any ideas why? Thank you for the help.

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

int main()
{
    int choice = 0;
    int location;
    string game;
    
    vector<string> games;
    vector<string>::iterator myIterator;
    vector<string>::const_iterator iter;
    
    do
    {
        cout << "1: Add a new game.\n";
        cout << "2: remove a game.\n";
        cout << "3: List all games.\n";
        cout << "4: Exit.\n";
        cin >> choice;
        switch (choice)
        {
             case 1:   
                  cout << "what is the title of the game you would like to add?\n";
                  cin >> game;
                  games.push_back(game);
                  break;
             case 2:
                  cout << "what is the title of the game you would like to remove?\n";
                  cin >> game;
                  if (find (games.begin(), games.end(), game) != string::npos)
                  {
                      myIterator = find(games.begin(), games.end(), game);
                      games.erase(myIterator);
                  }
                  break;
             case 3:
                  for (iter = games.begin(); iter != games.end(); iter++)
                  {
                      cout << *iter;
                  }
                  break;   
        }
    } while(choice != 4);
return 0;
}
//...
case 2:
  cout << "what is the title of the game you would like to remove?\n";
  cin >> game;
  myIterator = find( games.begin(), games.end(), game ) ;
  if ( myIterator != games.end() )
  {
    games.erase(myIterator);
  }
  break;
// ...
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.