• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

Very embarrassing C++ problem

gamepad

Golden Member
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.
 
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.
 
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.
 
Every command line noob has that problem.

Welcome to the world of Windows/DOS box programming.
 
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. 🙂
 
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.
 
Borland C++ 5.0

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

int main()
{

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

getchar();
return 0;
}
 
Or you can just set the IDE flag that keeps the DOS box open after program termination instead of modifying your program...
 
Back
Top