Huh? If you close the console window, the program should be terminated as well, and then the memory is reclaimed by the OS anyways. While having memory leaks at the end of the program is certainly a bad sign, usually they only cause problems at runtime.
The Windows console has some significant issues attached to it -- one of which is that programs are simply terminated if you close the console window. This is, however, the only reasonable way to do it without other a priori knowledge, since console programs are not all self-terminated the same way. It is possible to put a hook on the "close" action, but this is both OS-specific and it is broken (and unpatched) on a number of older versions of Windows.
If you are working with mission-critical cleanup stuff, you are best-off avoiding the Windows console all-together.
I've been at school and work all day, and what do I come home to? The infamous best-way-to-keep-the-windows-console-open debate (or B.W.T.K.T.W.C.O.D. for short) :P
On topic clarification: My teacher wants us to take a screenshot of the program as it's running in the console and attach the .png, along with the .cpp, when we complete the assignments and email them to her. That's the only reason (as far as I can tell) that she wants us to keep the console open using system("pause"). She doesn't actually compile the source herself. (I know this because I just turned in an assignment with the screenshot showing the correct results, but then I changed the .cpp to do nothing but print "I am the Walrus, coo coo ca choo." 100 times. I got 100/100 points)
B.W.T.K.T.W.C.O.D. clarification: I have Code:Blocks installed on one computer running WinXP, and it apparently has code built in that keeps the console open upon completion, without the need for system() or cin.ignore(). I think I read a post in the stickied "Keeping console open" thread where someone said that console windows close automatically because "some IDE's are too stupid to know to keep them open". But the IDE was programmed by someone or some team of people to do that. Why wouldn't the programmers just fix that is subsequent versions of their IDE's, or at least provide an option somewhere to toggle it on or off?
EDIT: oh wait, lol. It was you.
Duoas wrote:
It is because your IDE is too stupid to know that a console application run from the IDE should have an automatic pause at the end.
(I know this because I just turned in an assignment with the screenshot showing the correct results, but then I changed the .cpp to do nothing but print "I am the Walrus, coo coo ca choo." 100 times. I got 100/100 points)
...console windows close automatically because "some IDE's are too stupid to know to keep them open"...
[Talking from a Visual studio point of view]
If you run the program with "Start Debugging", the console closes at the end of the run.
If you run the program with "Start Without Debugging", the console stays open with a press key to continue message.
So why the difference, Is it because the IDE is too stupid? the developers of the IDE forgot to make them work the same? I very much doubt it. When debugging you want to see exactly how the program will run on the system outside the IDE and in that environment Windows will close the console so the IDE will let the console close.
If you want to hold the program up at the end just put a break point on the return from main.
Wait wait wait - your teacher doesn't even look at your source? What the heck. I think you should call her out on it after all. It's not like a program is correct just because it gives the correct results in one particular scenario.
When I was at University (way back when), the lecturers where quite open about the fact that they did not necessarily look at all the code all the time. They have various systems that they used to check source code automatically and made random selections for a more detailed look.
Well, at our university it's organized like:
We have 1 prof that holds a lecture for ~100-150 people
We have several tutors that also hold lectures and answer questions, for roughly 15-30 people.
And then the tutors hire students for correcting home works, though they check those corrections themselves as well.