468,761 Members | 1,812 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,761 developers. It's quick & easy.

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

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
(bi************@hotmail.com)

Aug 7 '05 #1
7 4145
for *windows* you can use

#include <cstdlib>
int main(int,char*[]){
return system("pause");
}

Aug 7 '05 #2
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
(bi************@hotmail.com)


#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.
Aug 7 '05 #3
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
(bi************@hotmail.com)


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.
Aug 7 '05 #4
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
(bi************@hotmail.com)


Mike
Aug 7 '05 #5
On Sun, 07 Aug 2005 21:15:42 +0200, Tobias Blomkvist <vo**@void.void>
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
(bi************@hotmail.com)

#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/~a...FAQ-acllc.html
Aug 8 '05 #6
include <iostream>
....
system ("PAUSE");

This seems to do the trick.

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

BigBadMick
(bi************@hotmail.com)

Aug 9 '05 #7
#include <iostream>
int main(int argc, char **argv)
{

while(!cin.get()){}
}
"Miktor" <bi************@hotmail.com> wrote in message
news:11*********************@g47g2000cwa.googlegro ups.com...
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
(bi************@hotmail.com)

Aug 9 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by nick | last post: by
17 posts views Thread by MumboJumbo | last post: by
1 post views Thread by Teis Draiby | last post: by
1 post views Thread by Brad Wylie | last post: by
4 posts views Thread by Paul Hemans | last post: by
1 post views Thread by tshad | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by Marin | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.