I've made some C++ dos programs, and it runs fine, and does everything I want it to, but the moment it does the last line of code, it exits. I use Visual C++, and it stays on when I preview it in there, but after I compile it, and run it in the directory it was stored in, it exits right away after it's done.

Is there anyway to solve this? I've tried "System: Pause", but I think i'm doing it wrong.

Recommended Answers

All 8 Replies

Try adding the following line just before you return from main():


for example, a hello world program:

#include <iostream>
using namespace std;

int main()
cout << "Hello, world" << endl;
return 0;

If the "cin" doesn't work for you - try

return 0;

This may not help whatsoever - I use Dev C++ and this is how it is done.

Hope it helps.....

The MuddBuddha ->

"cin" is standard C++. If you have a C++ compiler it will work.


is a platform specific call. It's generally preferable to use portable solutions rather than platform specific where such a portable solution exists.

Good point - Bob.....

Err...I'm a beginner in this. I'm not sure if adding a getchar() before your program ends would help or not. ;)

Mr Gates, were any of these suggestions helpful? I take it you got it working? A simple user input at the end of the program should offset it automatically exiting.

Yes, they seem to work great. I'm sorry I didn't reply earlier. :x

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.