954,535 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Program is shutting down right after program is executed

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.

Mr Gates
Light Poster
36 posts since May 2003
Reputation Points: 13
Solved Threads: 0
 

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

cin.get();

for example, a hello world program:

#include
using namespace std;

int main()
{
cout << "Hello, world" << endl;
cin.get();
return 0;
}

Bob
Junior Poster
Team Colleague
129 posts since Feb 2003
Reputation Points: 15
Solved Threads: 2
 

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

system("PAUSE");
return 0;
}

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

Hope it helps.....

The MuddBuddha ->

MuddBuddha
Light Poster
44 posts since May 2003
Reputation Points: 10
Solved Threads: 1
 

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

system("pause");

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

Bob
Junior Poster
Team Colleague
129 posts since Feb 2003
Reputation Points: 15
Solved Threads: 2
 

Good point - Bob.....

MuddBuddha
Light Poster
44 posts since May 2003
Reputation Points: 10
Solved Threads: 1
 

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

red_evolve
Posting Whiz
313 posts since Jun 2003
Reputation Points: 53
Solved Threads: 1
 

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.

cscgal
The Queen of DaniWeb
Administrator
19,424 posts since Feb 2002
Reputation Points: 1,474
Solved Threads: 230
 

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

Mr Gates
Light Poster
36 posts since May 2003
Reputation Points: 13
Solved Threads: 0
 

no problem :)

cscgal
The Queen of DaniWeb
Administrator
19,424 posts since Feb 2002
Reputation Points: 1,474
Solved Threads: 230
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You