I'm having a problem with my C++ code and I haven't really found anything online that describes why i'm having this problem. Here is my code:
/*
Write a program using vectors and iterators that allows a user to main-
tain a list of his or her favorite games. The program should allow the
user to list all game titles, add a game title, and remove a game title.
*/
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main()
{
vector<string> gamesList;
gamesList.reserve(10);
vector<string>::const_iterator iter;
string menu = "1. List all games\n";
menu += "2. Add a game title\n";
menu += "3. Remove a game title\n";
menu += "4. Quit\n";
string newTitle = "", removeTitle = "";
int choice = 0;
while (choice != 4)
{
cout << menu;
cout << "\nYour choice: ";
cin >> choice;
switch (choice)
{
case 1:
for (iter = gamesList.begin(); iter != gamesList.end(); ++iter)
{
cout << *iter << endl;
}
cout << "\nList capacity is " << gamesList.capacity() << endl;
break;
case 2:
cout << "Please enter a game title :";
cin >> newTitle;
gamesList.push_back(newTitle);
break;
case 3:
cout << "Which game title do you want to remove?\n";
cin >> removeTitle;
for (int i = 0; i < gamesList.size(); ++i)
{
if (gamesList[i] == removeTitle)
{
gamesList.erase(gamesList.begin() + i);
}
}
break;
case 4:
cout << "Good bye!";
break;
}
}
return 0;
}
If I run the program and enter Pong, Breakout and Tetris to the list, it works fine. If I run the program and enter Half Life or any title longer than 8 characters, the program goes into an infinite loop. Any help would be greatly appreciated.