I'm having a problem with a program.
How can i store two words in the addGame vector? When in case 2 of the switch, if i try to add like "hello world" without the quotation marks the program flips out and starts looping.
When i debug addGame is only storing the first of the words, like in the example
only "Hello".
I can add single words without a problem. And if i add something to addGame in the code with push_back it can store both words with the space in between them.
#include <iostream>
#include <string>
#include <vector>
#include <cstdlib>
#include <algorithm>
usingnamespace std;
int main()
{
vector<string> gameList;
vector<string>::const_iterator iter;
string addGame;
int removeNumber;
int choice;
cout << "Welcome to your personal game list!\n" << endl;
mainMenu:
int number = 0;
cout << "\n1. View the list." << endl;
cout << "\n2. Add a game to the list." << endl;
cout << "\n3. Remove a game from the list.\n\n" << endl;
cout << "Choose: ";
cin >> choice;
switch(choice)
{
case 1: for(iter = gameList.begin(); iter != gameList.end(); iter++)
{
cout << "\n" << *iter << endl;
}
goto mainMenu;
case 2: cout << "\nWrite the title of the game:";
cin >> addGame; //Here is where it fucks up in the program if i add a two word title.
gameList.push_back(addGame);
sort(gameList.begin(), gameList.end());
cout << "\nGame added succesfully." << endl;
goto mainMenu;
case 3:
for(iter = gameList.begin(); iter != gameList.end(); ++iter)
{
++number;
cout << number << ". " << *iter << endl;
}
cout << "\nType the number of the game you want to remove below." << endl;
cout << "Remove number: ";
cin >> removeNumber;
gameList.erase((gameList.begin() + removeNumber - 1));
cout <<"\nGame removed." << endl;
number = 0;
for(iter = gameList.begin(); iter != gameList.end(); ++iter)
{
++number;
cout << number << ". " << *iter << endl;
}
goto mainMenu;
}
return 0;
}
Quote from the reference on this site about the extraction operator
operator>>
istream& operator>> (istream& is, string& str);
Extract string from istream
Extracts a string from the input stream is storing its content in str. Any previous content of str is cleared.
This function overloads the global operator>> to behave as described in istream::operator>> but applied to string objects.
Notice that the istream extraction operations use whitespaces as separators, therefore this operation will only extract what can be considered a word from the stream. To extract entire lines of text, refer to the string overload of global function getline.
His problem has nothing to do with his choice to use strings nor his choice to use a vector (though there could have been a better choice of container). The problem was caused by his use of the extraction operator to get input from cin.
Had to put a cin.ignore() before the getline() too so it wouldn't skip it.
1 2 3 4 5 6 7 8 9
case 2: cout << "\nWrite the title of the game:";
cin.ignore();
getline(cin,addGame);
gameList.push_back(addGame);
sort(gameList.begin(), gameList.end());
cout << "\nGame added succesfully." << endl;
goto mainMenu;