Assert

From Tux
Revision as of 18:01, 23 January 2018 by Williams (talk | contribs) (Created page with "'''<code>assert</code>''' is a function available in the <code>cassert<code> header file that enables immediate termination of a program if an expression evaluates to false....")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

assert is a function available in the cassert<code> 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>

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;
}

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