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.
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
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.
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.
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 Entertwice 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...
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".
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)
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.'.