473,398 Members | 2,165 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

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 4493
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Oz | last post by:
This is long. Bear with me, as I will really go through all the convoluted stuff that shows there is a problem with streams (at least when used to redirect stdout). The basic idea is that my...
6
by: Dawn Minnis | last post by:
Hi (running Win xp and developing using Miracle C. Running applications in windows command prompt) I'm new to the group so be gentle with me. I am currently writing a C program to perform...
2
by: nick | last post by:
I am using C# do write a console program. Can I do the following tasks: 1. Detect if a key on keyboard is pressed? and get the typed ASC II code? 2. Catch and handle the event of the Control-C?...
5
by: Barry Mossman | last post by:
Hi, can I detect whether my class is running within the context of a Console application, vs say a WinForm's application ? also does anyone know whether the compiler or runtime is smart enough...
17
by: MumboJumbo | last post by:
Hi I have a really basic question hopefully some can help me with: Can you write a (i.e. one) C# project that works from the cmd line and gui? I seems if i write a GUI app it can't write to...
1
by: Teis Draiby | last post by:
I am working on a Win32 program with an additional console window to output messages. I also want to type commands in the console window. Is there any common way to achieve that? For example how...
1
by: Brad Wylie | last post by:
I have written a console project that uses a console window to display the output using Console.WriteLine. How do I keep the console window open after the writing the output as it seems to close...
4
by: Paul Hemans | last post by:
Hi newbie here. I am learning c++/cli I by writing console apps. When my apps finished they used to display a message at the bottom of the screen, something like "press any key to continue" so that...
1
by: tshad | last post by:
I noticed that when I run my program from "Start without Debugging", it will stop and wait for a key to struck. If I run my progoram from must "Start" it will not stop and will close the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.