954,136 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

How do I retain output window in Dev-C++

Dev-C++ v 4.9.9.2 IDE
When I compile and run my program as a console project, a window flashes very briefly on the screen and disappears. The compile log says compilation was successful and execution terminated.

How to I keep the window (my output window?) from disappearing?

rwkopcke
Newbie Poster
5 posts since Dec 2007
Reputation Points: 10
Solved Threads: 0
 

You have to add a line just before the end of main() to stop the program from closing. Most people call getch() or c++ cin.get(), which is just waiting for keyboard entry.

Ancient Dragon
Retired & Loving It
Team Colleague
30,041 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,341
 

You may want to have a look here http://www.daniweb.com/forums/thread90228.html .

Here's a snippet you can include at the end of main(), just prior to the return statement.

// exit routine
	cout << "\n\n...Press ENTER to Exit System...";
	cin.get();

or, if that doesn't work, pressing ENTER and nothing seems to happen, which is most likely due to the fact that cin refuses to accept any more characters due to it being in an error state and that error needs to be erased or cleared out; try this

cout << "...Press ENTER to Exit System...";
	cin.clear();
	while (cin.get() != '\n')
		;
	cin.get();

I'm still learning this stuff and most likely other more experienced forum members could better explain things. From my experience, reading up on cin and understanding why errors may occur (ie. assigning alphbetical characters to a numerical variable) will dictate what method you use.

And that's it, my first bit of advice, hope I'm on the mark.

superjacent
Junior Poster in Training
66 posts since Nov 2007
Reputation Points: 11
Solved Threads: 3
 

why should we not use system("pause");

rajatC
Junior Poster
109 posts since Dec 2007
Reputation Points: 14
Solved Threads: 7
 
why should we not use system("pause");


You are relying upon that program (pause.exe) being available and in that particular context it's only applicable or valid for Windows. I wrote a quick 'hello world' in Linux, it compiled ok, with system("pause") but on running it this is the output:>./test
sh: pause: not found
Hello World
If you do away with system("pause") then you can easily transfer your code and compile under other operating systems without having to make any modifications.

I'll re-state, I'm still learning this stuff and more experienced members might explain it better.

superjacent
Junior Poster in Training
66 posts since Nov 2007
Reputation Points: 11
Solved Threads: 3
 

i dont know this is correct or not but is there anything like this..

that when we use system("pause"); then the system remains in the wait state and is not free for any other process...but if we use cin.get(); then the system will do other processes too and will keep on checking the input stream (for any input entered by the user) after each instruction cycle of it's process...
this way the system will be able to do other processes too...it is kind of simulating many processes efficiectly making use of system's time..

@experts..
plz check is it correct or i m wrong about it..

rajatC
Junior Poster
109 posts since Dec 2007
Reputation Points: 14
Solved Threads: 7
 
..@experts.. plz check is it correct or i m wrong about it..

You are wrong. system("pause"); does not pause the operating system, although I suppose someone could write an operating system in which it would pause it but it would be pretty stupid to do that.

Ancient Dragon
Retired & Loving It
Team Colleague
30,041 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,341
 

cin.get(); works fine for compile and run. But when I choose simply to run again, after having compiled and run. The output window only flashes briefly again.

rwkopcke
Newbie Poster
5 posts since Dec 2007
Reputation Points: 10
Solved Threads: 0
 

>You are relying upon that program (pause.exe) being available
You're also relying on that program doing what you expect it to do. The problem is that one can easily replace pause.exe with another program of the same name but a malicious payload. system("pause"); isn't portable, but the real kicker is that it's a huge security hole.

>But when I choose simply to run again, after having compiled
>and run. The output window only flashes briefly again.
That sounds like an order of operations thing, though it's hard to say without knowing what compiler you use. :icon_rolleyes: You may be running an old version of the program that didn't have a pause at the end. Always compile before you run the program to avoid this problem.

Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 

Ok...thanks...

rajatC
Junior Poster
109 posts since Dec 2007
Reputation Points: 14
Solved Threads: 7
 

As a result of your advice and suggestions, with the Div-C++ IDE I have found that the following code keeps the output window open until it is released. And, after closing the window, this solution allows the compiled code to be rerun (without compiling again), producing an output window that remains open until it is released. I take your advice about the potential problems of using pause -- portability and security.

cout << "\n\n...Press ENTER to Exit System...";
cin.get();
return 0;

rwkopcke
Newbie Poster
5 posts since Dec 2007
Reputation Points: 10
Solved Threads: 0
 

just use getchar(); with conio.h header file...

it'll retain your screen.

for c users.

bvbvcb
Newbie Poster
1 post since May 2010
Reputation Points: 9
Solved Threads: 0
 

just use getchar(); with conio.h header file...

it'll retain your screen.

for c users.


Three years too lateand a wrong answer. Brilliant!

Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You