[SOLVED] How do I exit the code at a certain breaking point in a function

Issue

In this function to calculate the factorial of a number:

int fact(int n) {
    
    if (n == 0 || n == 1)
        return 1;
    else
        return n * fact(n - 1);
}

How do I add a condition

if(n<0)
cout <<"Error negative values are not accepted";
//Here I want to add something that makes me exit the function and stop the code after the cout statement is printed

Solution

You can use std::exit() after the cout statement

Answered By – FadedYoussef

Answer Checked By – Jay B. (BugsFixing Admin)

Leave a Reply

Your email address will not be published. Required fields are marked *