Thread: cin.get()

  1. #1
    Registered User
    Join Date
    Jul 2006
    Posts
    3

    cin.get()

    another cin. question but i was wondering exactly how this worked because on this code from the tutorial....

    Code:
    #include <iostream>
    
    using namespace std;
    
    void playgame();
    void loadgame();
    void playmultiplayer();
    	
    int main()
    {
      int input;
      
      cout<<"1. Play game\n";
      cout<<"2. Load game\n";
      cout<<"3. Play multiplayer\n";
      cout<<"4. Exit\n";
      cout<<"Selection: ";
      cin>> input;
      switch ( input ) {
      case 1:            // Note the colon, not a semicolon
        playgame();
        break;
      case 2:            // Note the colon, not a semicolon
        loadgame();
        break;
      case 3:            // Note the colon, not a semicolon
        playmultiplayer();
        break;
      case 4:            // Note the colon, not a semicolon
        cout<<"Thank you for playing!\n";
        break;
      default:            // Note the colon, not a semicolon
        cout<<"Error, bad input, quitting\n";
        break;
      }
      cin.get();
    }
    ...I have to put in cin.get inbetween cout<< and break inorder for it to work (yes i know the methods are undifined but in my actual code i made it a funcional program)

  2. #2
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Just put a cin.ignore() after the "cin >>" statement. It leaves a newline in the buffer which you have to remove before the final cin.get(), which is why it's been closing on you.
    Sent from my iPadŽ

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, 10: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, 01:26 AM