Thread: Console Window Dissapears after hitting Enter

  1. #1
    For Narnia! Sentral's Avatar
    Join Date
    May 2005
    Location
    Narnia
    Posts
    719

    Exclamation Console Window Dissapears after hitting Enter

    I made a very simple program here:

    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    
    // Prompt the user to enter a name, collect the name,
    // and display a message to the user that contains
    // the name.
    int main()
    {
        string userName;
    
        cout << "What is your name? :";
        cin >> userName;
        cout << "Hello " << userName << "!" << endl;
        cin.get();
        return 0;
    }

    After I put my name in, I push enter, then the window dissapears. Why? Thanks in advance.
    Videogame Memories!
    A site dedicated to keeping videogame memories alive!

    http://www.videogamememories.com/
    Share your experiences with us now!

    "We will game forever!"

  2. #2
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398
    See the Programming FAQ.

    This is a "feature" of Windows. It closes the window when the program is done.

  3. #3
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    When you entered the input, the '\n' was left in the stream. Hence, cin.get() extracted the '\n' and the program ended. Add cin.ignore() or another cin.get() before cin.get().
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  4. #4
    For Narnia! Sentral's Avatar
    Join Date
    May 2005
    Location
    Narnia
    Posts
    719
    Thank you JaWib. I took your advice and put another cin.get(); before my other one and it WORKED.
    Videogame Memories!
    A site dedicated to keeping videogame memories alive!

    http://www.videogamememories.com/
    Share your experiences with us now!

    "We will game forever!"

  5. #5
    Registered User
    Join Date
    May 2005
    Location
    Germany
    Posts
    12
    One line before "return 0;" you should write:

    System("pause");

    The Console will wait to a second hit of the return key.
    Last edited by kapri; 05-29-2005 at 09:40 AM.

  6. #6
    Weak. dra's Avatar
    Join Date
    Apr 2005
    Posts
    166
    you can also replace cin.get() with getch(). it'll keep the console window open until you press a key. but'd you'd have to include conio.h to use it.
    Last edited by dra; 05-29-2005 at 02:34 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. WM_CAPTION causing CreateWindowEx() to fail.
    By Necrofear in forum Windows Programming
    Replies: 8
    Last Post: 04-06-2007, 08:23 AM
  2. creating a child window
    By rakan in forum Windows Programming
    Replies: 2
    Last Post: 01-23-2007, 03:22 PM
  3. Adding colour & bmps to a Win 32 Window??
    By carey_sizer in forum Windows Programming
    Replies: 4
    Last Post: 09-04-2004, 05:55 PM
  4. Problem with creating new window, from another window
    By Garfield in forum Windows Programming
    Replies: 6
    Last Post: 01-11-2004, 02:10 PM
  5. keeping console window open
    By Unregistered in forum C++ Programming
    Replies: 4
    Last Post: 07-18-2002, 07:15 PM