cin.get() being ignored

This is a discussion on cin.get() being ignored within the C++ Programming forums, part of the General Programming Boards category; Hi Im using a switch case for menu options.In case 1 im just printing the values of a couple varibles.I ...

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    38

    cin.get() being ignored

    Hi
    Im using a switch case for menu options.In case 1 im just printing the values of a couple varibles.I put in the getline() but it seems to be by passed all together.

    the varible flashs up on the screen as it should with out the getline().
    if i use system("pause") within the switch case it stops the screen so i can make sure the vars are returning the correct values.Also if i put the getline() outside the switch case...

    ..i know the problem aint exactly the end of the world just curious whats not adding up,

    regards C

    Code:
    switch(x){
    			case 1:
    				playerStats(  playerName, gold, playerTea ,playerFood,playerSpices, playerLoc);//just cout's stats	
    				cin.get();
    				break;
    
    			case 2:
    				//sell function
    				break;
    
    			case 3:
    				//change location
    				break;
    
    			case 4:
    				return 0;
    				break;
    
    			default:
    				cout<<"YoU bRoKe ThE CoMpUtEr!!!"<<endl;
    				cin.get();
    				return 0;
    				break;
    		}

  2. #2
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,656
    If getline doesn't wait for you to type something that means it's consuming stray input.

    If you handle your input like this

    std::cin >> foo;
    std::cin.ignore();

    it should fix the immediate problem. It's a better idea not to mix the getline function with stream extraction though, since they behave differently (getline reads anything, extraction skips over white space by default). If you can avoid that, do so.

  3. #3
    Registered User
    Join Date
    Feb 2009
    Posts
    38
    thanks,I get you,its working now

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. cin.get(); not working with my switch statement
    By tenor_jazz13 in forum C++ Programming
    Replies: 2
    Last Post: 01-12-2008, 09:33 PM
  2. cin.get() problem
    By Cilius in forum C++ Programming
    Replies: 20
    Last Post: 07-28-2005, 05:32 PM
  3. Confused about cin.get(); and classes.
    By RaccoonKing in forum C++ Programming
    Replies: 6
    Last Post: 07-17-2005, 11:44 AM
  4. cin.get();
    By swgh in forum C++ Programming
    Replies: 2
    Last Post: 06-29-2005, 07:51 AM
  5. curiosity about cin.get() and cin.getline()
    By ssjnamek in forum C++ Programming
    Replies: 18
    Last Post: 11-30-2003, 12:26 AM

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