Difference between revisions of "Assert"

From Tux
Jump to: navigation, search
Line 4: Line 4:
 
#include <iostream>
 
#include <iostream>
 
#include <cassert>
 
#include <cassert>
 +
 +
using namespace std;
  
 
int main()
 
int main()

Revision as of 18:03, 23 January 2018

assert is a function available in the cassert header file that enables immediate termination of a program if an expression evaluates to false. It can be used to exit from a program at any point, even inside a function.

#include <iostream>
#include <cassert>

using namespace std;

int main()
{
     cout << "Enter numerator: ";
     double numerator;
     cin >> numerator;

     cout << "Enter denominator: ";
     double denominator;
     cin >> denominator;

     assert(denominator != 0);

     cout << "Result is: " << numerator / denominator << endl;

     return 0;
}

The result of the above program being run and the input for the denominator being 0 will be: