Usage of exit()

Viewed 76

I've found this code and I don't understand why they end the program with exit(0) when the input is not valid. Why they didn't use exit(1)? I've heard that we should use exit(1) when there is a failure. So when I should use EXIT_FAILURE or EXIT_SUCCESS?

#include<iostream>
using namespace std;
int main()
{
    int i;
    cout<<"Enter a non-zero value: ";  //user input
    cin>>i;
    if(i)    // checks whether the user input is non-zero or not
    {
        cout<<"Valid input.\n";
    }
    else
    {
        cout<<"ERROR!";  //the program exists if the value is 0
        exit(0);
    }
    cout<<"The input was : "<<i;
}
1 Answers

There is no reason for them to use exit(0) in that situation. Maybe they just wanted the program to exit gracefully despite the incorrect input. Don't waste time trying to understand their thinking behind that decision. You can use either whenever you feel it fits.

Related