Why does my program only stay open like this?

This is a discussion on Why does my program only stay open like this? within the C++ Programming forums, part of the General Programming Boards category; Code: #include <iostream> #include <string> int main() { std::cout << "Please enter your first name: "; std::string yoname; std::cin >> ...

  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
    a guy with long hair Xupicor's Avatar
    Join Date
    Sep 2010
    Location
    Poland
    Posts
    103
    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,527
    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 in reply to you, it is likely you deserve it. Suck it up, sunshine, 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, 06:13 AM
  2. Open with my program
    By elmutt in forum Windows Programming
    Replies: 12
    Last Post: 08-14-2007, 03: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, 06:38 PM

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