keyboard buffert.

This is a discussion on keyboard buffert. within the C++ Programming forums, part of the General Programming Boards category; Hi. I wrote a program that lets me put in 2 games, then the program display the 2 games and ...

  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    65

    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;
    }
    Last edited by And3rs; 10-18-2008 at 10:07 AM.

  2. #2
    Registered User
    Join Date
    Oct 2008
    Posts
    65
    comon, some1? :P

  3. #3
    Registered User
    Join Date
    Nov 2005
    Posts
    673
    Code:
    std::getline(std::cin,list);
    should work.

  4. #4
    Registered User
    Join Date
    Oct 2008
    Posts
    65
    well If I use that, the program will not delete a game

  5. #5
    Registered User
    Join Date
    Oct 2008
    Posts
    65
    now it did, lol..

    thanks! =)

  6. #6
    Registered User
    Join Date
    Oct 2008
    Posts
    65
    what does getline really do? that is so different from cin

  7. #7
    Registered User
    Join Date
    Nov 2005
    Posts
    673
    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"

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Doesn't stop at spaces?

  9. #9
    Registered User
    Join Date
    Oct 2008
    Posts
    65
    true tabstop ^^

    And thx raigne

  10. #10
    Registered User
    Join Date
    Nov 2005
    Posts
    673
    std::getline() you can choose the delimiting character. Can be used with std::cin, and also other streams, such as std:fstream.

    The third parameter is the delimiter.
    Code:
    //This one stops at the first 'a' character
    std::getline(std::cin,some_string,'a');

  11. #11
    Registered User
    Join Date
    Oct 2008
    Posts
    65
    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 ^^

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Detecting keyboard and mouse in linux
    By sameer.nalwade in forum C Programming
    Replies: 3
    Last Post: 11-22-2008, 04:24 AM
  2. Keyboard port using other that a keyboard
    By antoinelac in forum C++ Programming
    Replies: 4
    Last Post: 06-12-2008, 03:46 PM
  3. Virtual keys
    By Arkanos in forum Windows Programming
    Replies: 4
    Last Post: 12-12-2005, 10:00 AM
  4. Keyboard hook
    By joecaveman in forum Windows Programming
    Replies: 2
    Last Post: 09-03-2005, 09:07 AM
  5. Game Design Topic #2 - Keyboard or Mouse?
    By TechWins in forum Game Programming
    Replies: 4
    Last Post: 10-08-2002, 04:34 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21