2

Is there a line in c++ that allows me to terminate my program while I am not in main()?

For example, a code where if you die you have to quit the game? I don't want it to rely on the "trust" system.

ConMaki
  • 77
  • 1
  • 2
  • 5
  • http://en.cppreference.com/w/cpp/utility/program/exit – hmjd May 14 '13 at 14:59
  • 3
    `std::exit` is what you are looking for, this has however nothing to do with trust ... – filmor May 14 '13 at 15:00
  • http://stackoverflow.com/questions/4038302/how-do-i-make-a-c-console-program-exit – chris May 14 '13 at 15:00
  • 2
    As everyone is telling you, `exit` is the answer. But you could just as easily have some flag that shows the user has died, display somewhere "You have died", and simply refuse to accept any more user input. You're right - you should never rely on "You've died. Could you please leave now?" You're creating the game - that means you control everything the user can do and when they can do it as long as the game is running. – Scott Mermelstein May 14 '13 at 15:05

1 Answers1

6

Is there a line in c++ that allows me to terminate my program while I am not in main()?

I would not use the word "line" here - better say "function". For normal termination, you can use std::exit() (also see § 18.5/8 of the C++11 Standard):

#include <cstdlib>

void foo()
{
    std::exit(EXIT_SUCCESS);
}

int main()
{
    foo();
}

Here is a live example.

Andy Prowl
  • 124,023
  • 23
  • 387
  • 451