cin.get() inside switch statement

This is a discussion on cin.get() inside switch statement within the C++ Programming forums, part of the General Programming Boards category; This works, it pauses my program as desired: Code: std::cout << "\n [S]ave Report [O]pen Report"; std::cout << "\n [Q]uit" ...

  1. #1
    Registered User
    Join Date
    Oct 2003
    Posts
    12

    cin.get() inside switch statement

    This works, it pauses my program as desired:
    Code:
    	std::cout << "\n		[S]ave Report     [O]pen Report";
    
    	std::cout << "\n		[Q]uit" << std::endl;
    
    
    
    	std::cout << "\n		Enter Choice: ";
    
    		std::cin >> choice;
    
    
    
         switch(choice)
         {
              case 'i':
                   system ("clear");
                   one.ClearIt();
                   std::cout << "\n                Program has been set to defaults.";
                        std::cout <<"\n                 Press [Enter] to continue.";
                          std::cin.get();
                             std::cin.get();
              break;
              
              default:
                   break;
    This code displays the cout statements and goes back to the main loop:
    Code:
    	std::cout << "\n		[S]ave Report     [O]pen Report";
    
    	std::cout << "\n		[Q]uit" << std::endl;
    
    
    
    	std::cout << "\n		Enter Choice: ";
    
    		std::cin >> choice;
    
    
    
         switch(choice)
         {
              case 'i':
                   system ("clear");
                   one.ClearIt();
                   std::cout << "\n                Program has been set to defaults.";
                        std::cout <<"\n                 Press [Enter] to continue.";
                             std::cin.get(); 
              break;
              
              default:
                   break;
    Press [Enter] to continue is straight out of the programming faq, I compiled the example without fail, with only 1 cin.get() statement, My question is: why does the example work, when I put it into my program, it just skipped over it, how?

  2. #2
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    There may still be some characters sitting in stdin. You may have to account for a newline or something, I'm not too sure.

    On having a second glance, I would say that's it.
    Code:
    std::cin >> choice;
    This line likely takes only the char from stdin, leaving the newline there. Thus, std:cin.get() takes the newline. There is another thread at the moment concerning this problem, but I can't remember how they solved it.
    curiosity about cin.get() and cin.getline().
    benforbes@optusnet.com.au
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  3. #3
    Registered User
    Join Date
    Oct 2003
    Posts
    12
    By using 2 get() statements, I got my program to work, I have been struggling with this code because I wrote this program in windows and used some windows only stuff like __int64, a 64 bit integer,and the getch(), I am going to try long double in Linux, But I need a variable to hold 18 digits, I think I am going to do all my console programming in Linux from now and then port to windows instead of porting to linux.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Stack operations from switch statement elements
    By mlsrar in forum C Programming
    Replies: 15
    Last Post: 10-02-2008, 01:12 PM
  2. 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
  3. Switch statement
    By beene in forum C++ Programming
    Replies: 21
    Last Post: 07-01-2007, 08:13 AM
  4. Switch
    By cogeek in forum C Programming
    Replies: 4
    Last Post: 12-23-2004, 05:40 PM
  5. Uh-oh! I am having a major switch problem!
    By goodn in forum C Programming
    Replies: 4
    Last Post: 11-01-2001, 03:49 PM

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