Thread: After entering data and pressing "enter", console window disappears

  1. #1
    Registered User
    Join Date
    Mar 2008
    Posts
    2

    After entering data and pressing "enter", console window disappears

    I wrote a fairly simple programme:

    Code:
    #include <iostream>
    
    int main()
    
    {
        using namespace std;
        
        int books; // declares variable "books"
        
        cout << "how many books do you have?" << endl; // displays the first line
        cin >> books; // input a number 
        cout << "here are 100 more." << endl; // displays the second line
        books=books+100; // adds 100 to the variable "books"
        cout << "now you have " << books << " books." << endl; 
        //displays the third line with the changed variable "books"
        
        cin.get();
        return 0;
    }
    It successfully compiled. When run, it correctly displays the first line:

    how many books do you have?

    Then I entered a random number, like 500. I pressed "enter" then the console window immediately disappeared.

    My IDE is dev c++, OS is windows xp service pack 2

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Look in the FAQ about "my window disappears" or some such title.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    once you fully understand why you have put
    Code:
        cin.get();
    at the end of main(), you will understand why it fails in this case.
    Kurt

  4. #4
    Registered User
    Join Date
    Mar 2008
    Posts
    2

    reply

    I put

    Code:
    system ("pause");
    instead of cin.get();

    It worked.

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    System pause is unportable, so avoid that.
    Better to just use a proper IDE that supports not closing the window or use two or maybe three cin.get() rather than system pause.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,229
    System pause is unportable, so avoid that.
    True, but the thing is, you don't need to do this in anything other than Windoze. No other OS that I know of closes the terminal when a program quits.

    Maybe something like this?
    Code:
    #ifdef _WIN32
    system("pause");
    #endif
    But of course, there is a security risk associated with doing it this way, too, since the working directory is in $PATH in Windoze, and worse still, it precedes everything else. (if someone wrote a malicious program and name it pause.exe and put it into your working directory, it will be executed instead of the system's pause.exe)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Window menu Probem
    By elwad in forum Windows Programming
    Replies: 5
    Last Post: 05-11-2009, 03:14 AM