Hi! This is my first post, so be gentle.. :D

I am learning CPP with a book called "Beginning C++ Game Programming", and I have run into a problem.

The book asks me to make a program were the user can enter his/hers favorite games, delete a game, and print the list.

I am using vectors and iterations, and I cant seem to get the run-time error I am getting.
The error window says: "Expression: (this->_Has_containter()",0)

Here's my code:

if(command == "2")
		{
			string deleteGame;
			cout << "Enter name of game: " << endl;
			cin >> deleteGame;
			for(iter = gameList.begin(); iter != gameList.end(); ++iter)
				if(*iter==deleteGame)
					gameList.erase(iter);
		}

Recommended Answers

All 3 Replies

iter is invalid after calling erase, so you can't legally increment it. You need to be more careful about how you obtain the next iterator:

for (iter = gameList.begin(); iter != gameList.end(); ) {
    if (*iter == deleteGame)
        iter = gameList.erase(iter);
    else
        ++iter;
}

Thanks alot..
I figured that I could just break function when the if-statement was true, and that worked..

Now when I want to add a game, I use the string type variable, but if the game is multiple words, I only get the first word. What type of variable do I have to use, to have multiple words entered?

string newGame;
			cout << "\nEnter name of game: " << endl;
			cin >> newGame;
			gameList.push_back(newGame);

>cin >> newGame;
The >> operator stops reading when it encounters whitespace. If you want to read up to a specified character, you can use the std::getline function:

std::getline ( std::cin, newGame );

There's a magic third argument that defaults to '\n', but you can change that if you have a different delimiter. A pipe, for example:

std::getline ( std::cin, newGame, '|' );
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.