Hi everyone. I just got a compiler, and I made a "Hello World" type of program, but when I run it, I get a black screen that pops up for about a second, then it disappears. Help.
Printable View
Hi everyone. I just got a compiler, and I made a "Hello World" type of program, but when I run it, I get a black screen that pops up for about a second, then it disappears. Help.
post your code?
I think the code's right, because I used it straight from a guide, and I can compile it without errors, but here goes.
#include <iostream.h>
int main()
{
cout << "Hello World!\n";
return 0;
}
The problem comes to when I run it. A DOS screen pops up, disappears, and that's it.
Code:#include <iostream>
#include <conio.h> //I think this is the one with getch()
using namespace std;
int main()
{
cout << "Hello World!\n";
cout<<"Press any key to continue"<<endl;
getch();
return 0;
}
Yeah, that works. Why was that stuff added? Just linking me to an explanation would be fine.
getch() waits for a keypress i think , You could also just run your program in the command prompt. Start-> Run -> Command and then execute your program with that. When running the executable strait off, it does what its sopose to do then quits.
conio.h is the DOS header I beleive - and it contains a function called getch() which waits for a user to press a KEY.
after the key is pressed your program will continue which in this case returns 0; and your program exits.
All C++ programs act this way, once main() exits or returns a value the program ends, so you need to place something in between the return; statement if you don't want your program to end so quickly.
Ok, thanks. :D That really should be in the beginner's manuals.
Conio is nonstandard... better to vouch for something along the lines of:
Code:#include <iostream>
int main()
{
using namespace std;
cout << "Hello World!\n";
cin.get();
return 0;
}
better better way is to run in a real console window that dumps one back to shell prompt after termination.