943,476 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 5521
  • C++ RSS
May 30th, 2003
0

Program is shutting down right after program is executed

Expand Post »
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.
Reputation Points: 13
Solved Threads: 0
Light Poster
Mr Gates is offline Offline
36 posts
since May 2003
May 30th, 2003
0
Re: Program is shutting down right after program is executed
Try adding the following line just before you return from main():

cin.get();

for example, a hello world program:

#include <iostream>
using namespace std;

int main()
{
cout << "Hello, world" << endl;
cin.get();
return 0;
}
Bob
Team Colleague
Reputation Points: 15
Solved Threads: 2
Junior Poster
Bob is offline Offline
129 posts
since Feb 2003
May 30th, 2003
0

Reply

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 ->
Reputation Points: 10
Solved Threads: 1
Light Poster
MuddBuddha is offline Offline
44 posts
since May 2003
May 31st, 2003
0
Re: Program is shutting down right after program is executed
"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
Team Colleague
Reputation Points: 15
Solved Threads: 2
Junior Poster
Bob is offline Offline
129 posts
since Feb 2003
May 31st, 2003
0

Reply

Good point - Bob.....
Reputation Points: 10
Solved Threads: 1
Light Poster
MuddBuddha is offline Offline
44 posts
since May 2003
Jul 9th, 2003
0

Re: Program is shutting down right after program is executed

Err...I'm a beginner in this. I'm not sure if adding a getchar() before your program ends would help or not.
Reputation Points: 53
Solved Threads: 1
Posting Whiz
red_evolve is offline Offline
313 posts
since Jun 2003
Jul 9th, 2003
0

Re: Program is shutting down right after program is executed

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.
Administrator
Staff Writer
Reputation Points: 1422
Solved Threads: 162
The Queen of DaniWeb
cscgal is offline Offline
13,645 posts
since Feb 2002
Jul 10th, 2003
0

Re: Program is shutting down right after program is executed

Yes, they seem to work great. I'm sorry I didn't reply earlier. :x
Reputation Points: 13
Solved Threads: 0
Light Poster
Mr Gates is offline Offline
36 posts
since May 2003
Jul 10th, 2003
0

Re: Program is shutting down right after program is executed

no problem
Administrator
Staff Writer
Reputation Points: 1422
Solved Threads: 162
The Queen of DaniWeb
cscgal is offline Offline
13,645 posts
since Feb 2002

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: C++ and AI
Next Thread in C++ Forum Timeline: a outline would help





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC