-
Hello Program
I am just starting out with my C++ Programming. I got this book Sam's Teach yourself C++ in 21 days. Im making the hello program. When I run after its compiled and stuff, It pops up, shows for like a second or less and minimizes or closes. Here is the code. If there is a way to fix it please tell me and please explain to me what it does.
#include <iostream.h>
int main()
{
cout << "Hello World!\n";
return 0;
}
Thank you
-
I have a solution
From: http://www.cprogramming.com/close.html
"It is a common problem for your program's output window to close before you can see the result of the program. The simplest solution is to require the program to wait for the user to input a final keypress before closing. The simplest solution is to add the following code at the end of your program, just before the return 0;:
cin.get();
This code will cause your compiler to wait until you press a key before closing the program's output window."
Another thing i notace is there's no need for a \n (newline)
Hope that help's.
-
you might want to add system("cls"); to clear the screen also, you could use getch() to pause the screen, for example:
Code:
#include <iostream.h>
#include <stdlib.h>
#include <stdio.h>
int main()
{
system("cls"); // you can use clrscr() if your using dos compiler
cout << "Hello World!\n";
getch();
return 0;
}
-
when you are making console applications (at least when you are beginning), you should get out of the whole "point & click" mentality.
When you p&c, windows controls/opens your console, runs the code, and then shuts the console down (by default for 95/98/ME and perhaps XP OSes).
Open your console window first from your start menu and type the path/name of your executable.
Then you will see your display....
if my program is in C:\computer_newbie\bin\ and it is named hello.exe and my prompt looks like this....
C:\>
then I type
C:\>cd .\computer_newbie\bin
C:\computer_newbie\bin\>hello.exe
Hello world
C:\computer_newbie\bin\>
These are console applications. Learn how to use the console (dos) before following the standard advice of "pausing" your program to keep in the point & click mentality................. IMHO
-
Hmm, some compilers have the option to Run-Captured, which means it will keep the DOS box open when the code execution ends. But this is compiler specific.
-
Win NT and WIN2000 keep the console open after execution which is OS specific....
-
You don't need to use cls. Just put a system() call with pause