By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,853 Members | 1,046 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,853 IT Pros & Developers. It's quick & easy.

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

P: n/a
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
Share this Question
Share on Google+
7 Replies


P: n/a
for *windows* you can use

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

Aug 7 '05 #2

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
#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.