Thread: Why does my program only stay open like this?

  1. #1
    Registered User
    Join Date
    Jan 2009
    Posts
    8

    Why does my program only stay open like this?

    Code:
    #include <iostream>
    #include <string>
    
    int main()
    {
        std::cout << "Please enter your first name: ";
    
        std::string yoname;
        std::cin >> yoname;
    
        std::cout << "Hello, "<< yoname << "!" << std::endl;
        std::cin.ignore();
        std::cin.get();
    
        return 0;
    }
    If I use one or the other (std::cin.ignore() or std::cin.get()) then the program won't stay open. Does it really have to be a combination of the two?

    I'm using Code::Blocks 10.05 on Win7 64.

  2. #2
    Registered User
    Join Date
    Mar 2007
    Posts
    416
    std::cin>> yoname will leave the return key in the input buffer, cin.ignore() will then discard this return key. cin.get() then waits until another return key is pressed.

  3. #3
    Nasal Demon Xupicor's Avatar
    Join Date
    Sep 2010
    Location
    Poland
    Posts
    179
    You could just run the application in terminal, as it is a console app, and see, that it doesn't disappear at all.

    Or use whatever method to pause it at the end, just make sure to get rid of it in the final build. You wouldn't want "dir/ls", cd or pwd to have a "press enter to continue" at the end every time, hm?

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by scwizzo View Post
    std::cin>> yoname will leave the return key in the input buffer, cin.ignore() will then discard this return key. cin.get() then waits until another return key is pressed.
    That assumes the user does not enter any whitespace characters, other than carriage return after the first character input. The behaviour is a little different if there is other whitespace in the input.

    As a rough rule, mixing input styles on a particular stream (eg streaming operators then stream's get() function) is not a good idea. Using ignore() can only partially recover from that, particularly if input is not quite what you expect.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Zoom
    By jma619 in forum C Programming
    Replies: 1
    Last Post: 09-13-2009, 05:13 AM
  2. Open with my program
    By elmutt in forum Windows Programming
    Replies: 12
    Last Post: 08-14-2007, 02:23 AM
  3. Open a new program from a C++ application
    By axr0284 in forum C++ Programming
    Replies: 9
    Last Post: 12-29-2004, 12:29 PM
  4. change this program to open a file
    By A F Army in forum C Programming
    Replies: 3
    Last Post: 04-05-2003, 05:25 PM
  5. Ghost in the CD Drive
    By Natase in forum A Brief History of Cprogramming.com
    Replies: 17
    Last Post: 10-12-2001, 05:38 PM