Skip to content

Latest commit

 

History

History
33 lines (25 loc) · 796 Bytes

CppAssertExample1.md

File metadata and controls

33 lines (25 loc) · 796 Bytes

assert example 1: basics is an assert example.

A division will only succeed if the denominator is unequal to zero. In your code, you will have to take care that a division by zero never occurs. Using assert, as shown in the code below, will take you to the problem directly.

#include <cassert>
#include <iostream>

int main()
{
  const double numerator = 1.0;
  const double denominator = 0.0;
  assert(denominator != 0.0);
  const double result = numerator / denominator;
  std::cout << result << '\n';
}

Screen output:

Assertion failed!

Program: /my_path/my_program
File: my_path/main.cpp, Line 7

Expression: denominator != 0.0