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.
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?
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!
@ropez
That's brilliant!
@DanteTheDemon
Typo. Please read the rest of the thread.
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.