Console Closing Down

Pages: 123... 7
Hi, i am new to C++ and have just written my "Hello World" program. It worked all right but the console/cmd closes down instantly. If you tell me why it is closing down it would be very helpful.
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.

Add the following to the end of your main() function to fix it:
1
2
3
4
5
6
7
  ...

  std::cout << "Press ENTER to continue...";
  std::cin.ignore( std::numeric_limits<std::streamsize>::max(), '\n' );

  return 0;
  }

You'll need to #include <limits>

Enjoy.
Zolaboony,

You can also use:

system ("PAUSE");

Make sure to input above in the last line of "main" just before the ending brace. This will require you to "Press any key...." to close the window.

Personally(I am new to programming too), I use the following it lets me run the program again and again to test different values.

int main () {

while (true) { //This repeats the program. You close console window manually by clicking "x".

system ("CLS"); //this will clear the screen of any text from prior run
cin.clear(); //this will clear any values remain in cin from prior run

body of your program goes here

system ("PAUSE");
} // this ends while loop
return 0;
} // this ends your main function.

I hope this helps.

Last edited on
Grumble.

Don't use system("anything"). It is slow. It is disliked by antivirus software. It is OS-dependent. It tags you as an absolute beginner. And it is a massively huge, gaping, ugly security hole.

If all you are doing is some silly homework assignment or playing with your own code, it is fine. But for everything else: please, please don't.
I`ll tell the most short explain:
just before closing- return 0; type
system("pause");
-----------------------------------------------------------------------------------------------
but remember that if you go to competition you must always delete it
Did you even read the rest of this thread?
Duoas,

sorry man, I didn't meant to upset or lead anyone in wrong direction. I am an absolute beginner. Just finished the functions chapter and now going into arrays. I didn't know "system.." can cause security problems, but now i know. Thanks.
Don't feel bad. It just irks me that new programmers are constantly taught to do something like that by people that ought to know better.

There's nothing wrong with using system() when used properly. And, like I said, just for some homework assignment or just dinking around on your own it's fine (I've done it myself). But for finished, production code, it is usually a no-no.

The related one is the "cls" (or "clear" in Unix/Linux). The problem with that one is that it encourages people to use it trivially, when it is in fact an OS-specific (and often terminal-specific) thing to do. In other words, it presupposes some knowledge about the output device, which typically makes command-line programs less friendly by restricting the I/O to some human sitting in front of a display and keyboard.

But again, there are legitimate reasons to do it, such as a console text editor or the like.

To end my rant... one-size-fits-all, easy solutions that teach nothing and defeat security irk me.

:-)
An even simpler way to keep the console from closing immediately after the code has run is to declare an int (for example 'i') then put the following line just before the 'return 0;' :-

std::cin >> i;

All you have to do then to close the console is input a number when you're ready.
Or just run your program from the command line?
@Mark Harrigan
std::cin >> i;

How is that simpler than cin.ignore() ?

It is bad UI because (1) it doesn't tell the user that he is expected to do something, (2) it requires him to input a specific thing in addition to pressing ENTER, and (3) it requires your program to have an extra variable lying around.

Again, like system() I've no objection to it so long as it doesn't make it out into the real world where it will confuse people and allow them to abnormally terminate your program.

@Lodger
Yes, but the OP's problem is with the IDE, not the command-line.
You could also write
1
2
cin.get();
cin.get(); 
just before return 0;
Last edited on
Hello zooobany ^_^,
you can also use:
you will need #include<iostream.h>
and then Add the following to the end of your main()
....

getche();
}

good luck
Sigh. I suppose I am the only one here who isn't determined to turn this into the How to Do Things the Wrong Way thread. Either that or I'm the only one who has bothered to read more than the first post.

@nilsson
So the user still doesn't get any instruction on what to do, but now he has to press Enter twice to quit? Do you mind if I just use Ctrl-C ?

@RIZKY
That there non-standard, platform-dependent function is found in <conio.h>. Of course, <iostream.h> is non-standard too...
yeah, using Dev-C++ Or Delphi Makes It Real Dumb About Hello World.
Good Luck On Your C++, Though!
First I would search for the setting in my IDE that says "Keep console window open after program terminates".

If I didn't find it, I'd send a request for it to be included in the next version.

In the meantime: All the suggestions so far assumes that the program reach the end of main. Neither of you considered exceptions, or something like this in main:
1
2
if (somethingFailed())
  return SOME_ERROR_CODE;


I suggest creating this small tool:
1
2
3
4
5
6
7
8
class KeepRunning {
  public:
    ~KeepRunning() {
      // Whatever code you prefer to keep the program running, e.g.
      system("pause"); // "Press enter"
      sleep(5); // Wait 5 seconds
    }
};


In your main function:

1
2
3
4
5
6
int main() {
  KeepRunning kr;

  // code

} // Program stops here 


This way the program must keep the console open, even if it doesn't reach the end of main.

BTW: Of course, remember to remove this code before the program is "released".
At the end just put
 
cin.get;


This will nto only make you have to press enter to exit, but it doesnt actually show the "Please press enter to continue" (Not the one you put in your code)
@ropez
That's brilliant!

@DanteTheDemon
Typo. Please read the rest of the thread.
@Duoas:
That's brilliant!

Glad you liked it.

The technique is similar to RAII:
http://en.wikipedia.org/wiki/Resource_Acquisition_Is_Initialization
Last edited on
Duoas,

For the benefit of us beginners, could you explain why you prefer ropez's solution when you've previously been rather categorical in your dislike of the use of system():

'Don't use system("anything"). It is slow. It is disliked by antivirus software. It is OS-dependent. It tags you as an absolute beginner. And it is a massively huge, gaping, ugly security hole.'.

Many thanks.
Pages: 123... 7