-
keyboard buffert.
Hi.
I wrote a program that lets me put in 2 games, then the program display the 2 games and ask me what game I would like to delete.
The problem occurs when im trying to write a game with two names (king kong for example)
the program thinks that the whitespace between the names means that im done with my first game.
so the program displays that I have 2 games and they are named king and kong.
How do I ignore that whitespace?
Code:
int main()
{
string list;
string list1;
vector<string> gList;
vector<string>::iterator game;
vector<string>::const_iterator myGame;
cout << "Please insert a game you would like to add: ";
cin >> list;
gList.push_back(list);
cout << "Please insert another game: ";
cin >> list1;
gList.push_back(list1);
cout << "you got " << gList.size() << " games.";
for(game = gList.begin(); game != gList.end(); ++game)
cout << "\nAnd they are: " << " " << *game;
getch();
system("cls");
string choice;
cout << "Would you like to remove " << list << " or " << list1 << "?";
cin >> choice;
if(choice == list)
{
gList.erase(gList.begin());
}
else if(choice == list1)
{
gList.erase((gList.begin()+1));
}
cout << "You now got " << gList.size() << " game left.";
for(myGame = gList.begin(); myGame != gList.end(); ++myGame)
cout << "\nAnd it is " << *myGame;
getch();
return 0;
}
-
-
Code:
std::getline(std::cin,list);
should work.
-
well If I use that, the program will not delete a game
-
now it did, lol..
thanks! =)
-
what does getline really do? that is so different from cin
-
Also just to be a pain.
Code:
cout << "You now got " << gList.size() << " game left.";
should be
Code:
if ( gList.size() > 1 )
cout << "You now have " << gList.size() << " games left.";
else
cout << "You now have 1 game left.";
Mainly just correcting your grammar there. "You now got" should be "You now have"
-
-
true tabstop ^^
And thx raigne
-
std::getline() you can choose the delimiting character. Can be used with std::cin, and also other streams, such as std::ofstream.
The third parameter is the delimiter.
Code:
//This one stops at the first 'a' character
std::getline(std::cin,some_string,'a');
-
I tried to write the code using char arrays before, because the program did not let me write 2 named games.. cin.getline(list,xxx) fixed that problem.
But did not now how to do it with strings, but now I do ^^