1

I want to know if you can end a program from within a void function. I know that with using void(), you can't just do return 0, so what is there that would allow me to end my program early?

Here is some code to help you understand my situation:

void checkInput(int a) {
    if (a is negative) {
        cout << "Your number can't be negative" << endl;
        // exit program
    }
    else {
        // move on to calculate square root function
    }
}

What I am trying to do here is simulate a problem where a function checkInput() will check and see if a number is negative before it goes onto the next function, which actually performs the square root of the number, so I want to make sure it's not negative. If it is negative, I want to exit out of the program. This is just an example program.

Niall
  • 30,036
  • 10
  • 99
  • 142
Cjolsen06
  • 89
  • 1
  • 2
  • 6

4 Answers4

3

You could use C's exit:

void checkInput(int a) {
  if (a is negative) {
    cout << "Your number can't be negative" << endl;
    exit (EXIT_FAILURE);
    ^^^^^^^^^^^^^^^^^^^^
  } else {
    // move on to calculate square root function
  }
}
101010
  • 41,839
  • 11
  • 94
  • 168
2

For an early return from the function

return;

For an early terminate of the process (abnormal termination)

std::terminate();

For an early exit ("normal" termination)

std::exit(0); // or some other error code

Ultimately, it may be better to define a flow control that allows the program to end after main has completed.

Niall
  • 30,036
  • 10
  • 99
  • 142
  • `std::terminate()` just calls `std::abort()` by default. Why the extra layer of indirection? – Dietrich Epp Feb 11 '16 at 08:37
  • Also, this means that static destructors won't get called. This could be very surprising. – Dietrich Epp Feb 11 '16 at 08:38
  • @DietrichEpp. Terminate has additional hooks, such as [`set_terminate`](http://en.cppreference.com/w/cpp/error/set_terminate) etc., but yes, the default will eventually call `abort`. – Niall Feb 11 '16 at 08:38
  • @DietrichEpp. Sure, but it sounds like the OP is looking for an abnormal exit based on an error condition. – Niall Feb 11 '16 at 08:45
  • There could still be reasons why you'd want destructors to run. The first answer here sums it up well: http://stackoverflow.com/questions/30250934/how-to-end-c-code/30251056#30251056 – Dietrich Epp Feb 11 '16 at 08:50
  • @DietrichEpp. Agreed. That is a nice answer. In the end you need to decide what the error is and how "bad" it is. I still think the best is to define a flow that allows exit out of `main`. There are no surprises then. – Niall Feb 11 '16 at 08:53
0

One keyword and a semicolon:

return;
Humam Helfawi
  • 19,566
  • 15
  • 85
  • 160
0

You can use the exit function. It takes a single argument, which is the return value for the program that will be returned to the shell. This function is included from <stdlib.h>

However, the better way would be to add a return value to checkInput since using a exit in the middle of a function is spaghetti code.

Eli Iser
  • 2,788
  • 1
  • 19
  • 29
  • The comment about spaghetti code is not really appropriate. If you are writing a command-line batch program, the best way to handle an error is often to just print a message and `exit(1)`. – Dietrich Epp Feb 11 '16 at 08:35
  • I agree if the `exit` calls are in the same scope/function, then it would be fine to call `exit` directly instead of skipping to the end of the function. But having an `exit` call in very function that might fail is exactly spaghetti code. – Eli Iser Feb 11 '16 at 09:27