C++ end of program

May 23, 2010 at 4:30pm
is there any way to have a program terminate at the end, like the system("pause") command pauses it, so would there be a system command to end the program?
May 23, 2010 at 4:44pm
What would be the point?
If you want to terminate the program, call exit(0) (or with another error code instead of 0).
The program automatically terminates at the end anyway (i.e. when main returns).
May 23, 2010 at 4:44pm
why dont you try the following line ?
 
std::cin.get();
May 23, 2010 at 5:10pm
Yes the command is return 0; if the program is successful.

I may be reading a bit too much into your question but you seem to be trying to write a condition for the user to quit the program before it finishes? Or give them an option to exit an infinite loop?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
int main()
{
   //code code code and hey look at that more code!
  //...
  //...

  cin<<done;

  if(done == 1)
{
   return 0;
}

  //More code if need be
  //...
  //...

return 0;
}
Last edited on May 23, 2010 at 5:11pm
May 23, 2010 at 6:10pm
As I understand it, the OP is looking for a function to terminate the program. The exit() function in <cstdlib> does that. However, the best practice is to properly terminate by returning from main().

Hope this helps.
May 24, 2010 at 2:24am
Hey thanks this worked. yes I was trying to end the program before it finished so you were right
Topic archived. No new replies allowed.