Very embarrassing C++ problem

gamepad

Golden Member
Jul 28, 2005
1,893
1
71
I posted this in another forum, but I think that forum might be dead. I decided to bring this problem to the attention of Anandtechers (w00t!).

I am just beginning my adventure into the world of programming and am having trouble with C++.

Whenever I copy paste example programs into Dev-C++ and compile and run, I only see the program for a split second before it disappears. If the program has input, it allows me to input information, but then it disappears again.

Look at this program for an example:

#include <iostream.h>

int main()
{

cout << "Hello, World!" << endl;

return 0;
}

When I run the program, it just flashes for a fraction of a second and disappears.

I'd be very grateful if someone can help me with this woe.
 

Idontcare

Elite Member
Oct 10, 1999
21,110
59
91
What exactly did you want it to do? Print "hello world" and then "hang" for a while with this message on the screen?

If so then you need to add a wait/pause/loopwhile-count or some such, otherwise the program appears to be doing exactly what you programmed it to do - print hello world and then close thereafter because all the code has been executed.
 

brandonb

Diamond Member
Oct 17, 2006
3,731
2
0
If you run it from a command prompt (the .exe after it compiles) it will display and bring you back to the command prompt when exiting.
 

EagleKeeper

Discussion Club Moderator<br>Elite Member
Staff member
Oct 30, 2000
42,589
5
0
Every command line noob has that problem.

Welcome to the world of Windows/DOS box programming.
 

gamepad

Golden Member
Jul 28, 2005
1,893
1
71
Originally posted by: brandonb
If you run it from a command prompt (the .exe after it compiles) it will display and bring you back to the command prompt when exiting.

Oh.. that makes sense. Good thing I have about a week of Linux experience under my belt. :)
 

statik213

Golden Member
Oct 31, 2004
1,654
0
0
Originally posted by: tfinch2
getchar() is your friend.

If it is not clear, throw a getchar() at the end of your program. This function call will wait until you press a key and return that key's value. So this should give you some time to see your output.
 

dababus

Platinum Member
Apr 11, 2000
2,555
0
0
Borland C++ 5.0

#include <iostream.h>
#include <stdio.h>

int main()
{

cout << "Hello, World!" << endl;

getchar();
return 0;
}
 

Aikouka

Lifer
Nov 27, 2001
30,383
912
126
Or if you open a command prompt first, the window will not automatically close :p.
 

exdeath

Lifer
Jan 29, 2004
13,679
10
81
Or you can just set the IDE flag that keeps the DOS box open after program termination instead of modifying your program...
 

gamepad

Golden Member
Jul 28, 2005
1,893
1
71
Originally posted by: exdeath
Or you can just set the IDE flag that keeps the DOS box open after program termination instead of modifying your program...

How do I do that?