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

M

Miktor

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
([email protected])
 
T

Tobias Blomkvist

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
([email protected])

#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
 
P

__PPS__

for *windows* you can use

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

Mark P

Miktor said:
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
([email protected])

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.
 
M

Mike Austin

Miktor said:
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
([email protected])

Mike
 
J

Jack Klein

Miktor sade:

#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++.

Please don't post non-standard extensions here.
 
J

Joe Bacigalupa

#include <iostream>
int main(int argc, char **argv)
{

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

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,810
Latest member
Kassie0918

Latest Threads

Top