Preventing the console window from closing when writing a console program in C++

Discussion in 'C++' started by Miktor, Aug 7, 2005.

  1. Miktor

    Miktor Guest

    At the minute I am using the following rather unwieldy function to
    prevent the console

    window in Dev C++ from closing before I can see the output from my
    program:

    // function to prevent the Dev C++ Console Window from closing
    // before I can see the program's output

    void keepConsoleWindowOpen ()
    {
    int any_number;
    std::cout << "\n\n";
    std::cout << "Please enter a number to end the program:";
    std::cout << "\n\n";
    std::cin >> any_number;
    }

    In order to prevent the console window from closing too soon, I call
    the above

    function with:

    keepConsoleWindowOpen ();

    This works, but I'm sure it's not the most elegant method to use. Does
    anybody else

    know of a better way to prevent the console window from closing too
    soon when writing

    a C++ program in Dev C++ ?

    Thanks in advance!

    BigBadMick
    ()
    Miktor, Aug 7, 2005
    #1
    1. Advertising

  2. Re: Preventing the console window from closing when writing a consoleprogram in C++

    Miktor sade:
    > know of a better way to prevent the console window from closing too
    > soon when writing
    >
    > a C++ program in Dev C++ ?
    >
    > Thanks in advance!
    >
    > BigBadMick
    > ()
    >


    #include <conio>

    int main()
    {
    getch();
    return 0;
    }

    I guess it's not the most portable way though.
    I think the same function exist in curses.h.

    Tobias
    --
    IMPORTANT: The contents of this email and attachments are confidential
    and may be subject to legal privilege and/or protected by copyright.
    Copying or communicating any part of it to others is prohibited and may
    be unlawful.
    Tobias Blomkvist, Aug 7, 2005
    #2
    1. Advertising

  3. Miktor

    __PPS__ Guest

    for *windows* you can use

    #include <cstdlib>
    int main(int,char*[]){
    return system("pause");
    }
    __PPS__, Aug 7, 2005
    #3
  4. Miktor

    Mark P Guest

    Re: Preventing the console window from closing when writing a consoleprogram in C++

    Miktor wrote:
    >
    > This works, but I'm sure it's not the most elegant method to use. Does
    > anybody else
    >
    > know of a better way to prevent the console window from closing too
    > soon when writing
    >
    > a C++ program in Dev C++ ?
    >
    > Thanks in advance!
    >
    > BigBadMick
    > ()
    >


    Version 4.9.9.2 of Dev-C++ inserts the following code by default when
    starting a console project:

    #include <cstdlib>
    #include <iostream>

    using namespace std;

    int main(int argc, char *argv[])
    {
    system("PAUSE");
    return EXIT_SUCCESS;
    }

    Presumably system("PAUSE"); is then the only required line for what you
    want.
    Mark P, Aug 7, 2005
    #4
  5. Miktor

    Mike Austin Guest

    Re: Preventing the console window from closing when writing a consoleprogram in C++

    Miktor wrote:
    > At the minute I am using the following rather unwieldy function to
    > prevent the console
    >
    > window in Dev C++ from closing before I can see the output from my
    > program:
    >
    > // function to prevent the Dev C++ Console Window from closing
    > // before I can see the program's output
    >
    > void keepConsoleWindowOpen ()
    > {
    > int any_number;
    > std::cout << "\n\n";
    > std::cout << "Please enter a number to end the program:";
    > std::cout << "\n\n";
    > std::cin >> any_number;
    > }
    >
    > In order to prevent the console window from closing too soon, I call
    > the above
    >
    > function with:
    >
    > keepConsoleWindowOpen ();
    >
    > This works, but I'm sure it's not the most elegant method to use. Does
    > anybody else
    >
    > know of a better way to prevent the console window from closing too
    > soon when writing


    std::cin.get()

    Portable, and doesn't need a dummy variable.

    > a C++ program in Dev C++ ?
    >
    > Thanks in advance!
    >
    > BigBadMick
    > ()


    Mike
    Mike Austin, Aug 7, 2005
    #5
  6. Miktor

    Jack Klein Guest

    On Sun, 07 Aug 2005 21:15:42 +0200, Tobias Blomkvist <>
    wrote in comp.lang.c++:

    > Miktor sade:
    > > know of a better way to prevent the console window from closing too
    > > soon when writing
    > >
    > > a C++ program in Dev C++ ?
    > >
    > > Thanks in advance!
    > >
    > > BigBadMick
    > > ()
    > >

    >
    > #include <conio>


    There is no header named conio in standard C or standard C++.

    >
    > int main()
    > {
    > getch();
    > return 0;
    > }
    >
    > I guess it's not the most portable way though.
    > I think the same function exist in curses.h.


    There is no header named curses.h in standard C or standard C++.

    > Tobias


    Please don't post non-standard extensions here.

    --
    Jack Klein
    Home: http://JK-Technology.Com
    FAQs for
    comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
    comp.lang.c++ http://www.parashift.com/c -faq-lite/
    alt.comp.lang.learn.c-c++
    http://www.contrib.andrew.cmu.edu/~ajo/docs/FAQ-acllc.html
    Jack Klein, Aug 8, 2005
    #6
  7. Miktor

    Miktor Guest

    include <iostream>
    ....
    system ("PAUSE");

    This seems to do the trick.

    Cheers for all the advice chaps! Much appreciated. :)

    BigBadMick
    ()
    Miktor, Aug 9, 2005
    #7
  8. #include <iostream>
    int main(int argc, char **argv)
    {

    while(!cin.get()){}
    }


    "Miktor" <> wrote in message
    news:...
    > At the minute I am using the following rather unwieldy function to
    > prevent the console
    >
    > window in Dev C++ from closing before I can see the output from my
    > program:
    >
    > // function to prevent the Dev C++ Console Window from closing
    > // before I can see the program's output
    >
    > void keepConsoleWindowOpen ()
    > {
    > int any_number;
    > std::cout << "\n\n";
    > std::cout << "Please enter a number to end the program:";
    > std::cout << "\n\n";
    > std::cin >> any_number;
    > }
    >
    > In order to prevent the console window from closing too soon, I call
    > the above
    >
    > function with:
    >
    > keepConsoleWindowOpen ();
    >
    > This works, but I'm sure it's not the most elegant method to use. Does
    > anybody else
    >
    > know of a better way to prevent the console window from closing too
    > soon when writing
    >
    > a C++ program in Dev C++ ?
    >
    > Thanks in advance!
    >
    > BigBadMick
    > ()
    >
    Joe Bacigalupa, Aug 9, 2005
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?U2lsZWVzaA==?=
    Replies:
    4
    Views:
    33,899
    =?Utf-8?B?U29sZWwgU29mdHdhcmU=?=
    Oct 14, 2004
  2. Robert Mazur
    Replies:
    2
    Views:
    460
    Robert Mazur
    Oct 29, 2003
  3. =?Utf-8?B?Vk1J?=

    Closing popup window when closing parent window?

    =?Utf-8?B?Vk1J?=, Feb 14, 2007, in forum: ASP .Net
    Replies:
    3
    Views:
    652
    Thomas Hansen
    Feb 15, 2007
  4. Replies:
    5
    Views:
    3,429
    James Kanze
    Mar 4, 2008
  5. thomas
    Replies:
    0
    Views:
    226
    thomas
    Oct 23, 2003
Loading...

Share This Page