How to Write Assert and Continue in Main Win32

C++ assert

Introduction to C++ assert

The statements in C++ used to test the assumptions that the programmer in the program has made are called assertions in C++ like the array index being greater than zero is one of the examples of assertions, and when these assumptions are true in the program, the ending of the program in which these assertions are made is normal if not the program in which these assertions are made ends abnormally with an error message consisting of the condition that is failed, the name of the file consisting of the code and the line number and a preprocessor macro called assert is used to declare an assertion in the program which takes the condition to be evaluated as the parameter.

Syntax:

            assert(condtion)          

Where condition represents the condition to be evaluated in the program.

Working of assert function in C++

  • The statements in C++ used to test the assumptions that the programmer in the program has made are called assertions in C++, like the array index is greater than zero is one of the examples of assertions.
  • When the assumptions made in the program are true, the ending of the program in which these assertions are made is normal; if not, the program in which these assertions are made ends abnormally.
  • When the program in which assertions are made ends abnormally, an error message is displayed consisting of the condition that is failed, the name of the file consisting of the code, and the line number.
  • A preprocessor macro called assert is used to declare an assertion in the program, which takes the condition to be evaluated as the parameter.

Example of C++ assert

Given below are the examples of C++ assert:

Example #1

C++ program to demonstrate assert function that evaluates if an assumption made by the programmer is true or false and displays the output accordingly:

Code:

            //the two headers iostream and cassert are included to be able to make use of cin, cout and assert functions #include <iostream> #include <cassert> using namespace std; //main method is called int main() { //an integer variable called num is defined and the user is prompted to enter a value less than or equal to 5 int num; cout << "Enter an integer value less than or equal to 5: " << endl; cin >> num; cout << "The value entered is: " << num << endl; //assert function is used to check if the assumption made by the user that the user enters a value less than or equal to 5 is true or false assert(num <= 5); return 0; }          

Output:

C++ assert output 1

In the above program, the two headers, iostream, and cassert are included to be able to make use of cin, cout, and assert functions. Then the main method is called, within which an integer variable called num is defined, which stores the input value entered by the user. Then the assert function is called to check if the assumption made by the user that the user enters a value less than or equal to 5 is true or false. If it is true, the program ends normally; otherwise ends with an error message, as shown in the snapshot above.

Example #2

C++ program to demonstrate assert function that evaluates if an assumption made by the programmer is true or false and displays the output accordingly:

Code:

            //the two headers iostream and cassert are included to be able to make use of cin, cout and assert functions #include <iostream> #include <cassert> using namespace std; //main method is called int main() { //an integer variable called num is defined and the user is prompted to enter a value less than or equal to 5 int num; cout << "Enter an integer value less than or equal to 5: " << endl; cin >> num; cout << "The value entered is: " << num << endl; //assert function is used to check if the assumption made by the user that the user enters a value less than or equal to 5 is true or false assert(num <= 5); return 0; }          

Output:

C++ assert output 2

In the above program, the two headers, iostream, and cassert are included to be able to make use of cin, cout, and assert functions. Then the main method is called, within which an integer variable called num is defined, which stores the input value entered by the user. Then the assert function is called to check if the assumption made by the user that the user enters a value less than or equal to 5 is true or false. If it is true, the program ends normally as shown in the snapshot above otherwise ends with an error message.

Example #3

C++ program to demonstrate assert function that evaluates if an assumption made by the programmer is true or false and displays the output accordingly:

Code:

            //the two headers iostream and cassert are included to be able to make use of cin, cout and assert functions #include <iostream> #include <cassert> using namespace std; //main method is called int main() { //an integer variable called num is defined and the user is prompted to enter a value less than or equal to 5 int num; cout << "Enter an integer value less than or equal to 5: " << endl; cin >> num; cout << "The value entered is: " << num << endl; //assert function is used to check if the assumption made by the user that the user enters a value less than or equal to 5 is true or false assert(num <= 5); return 0; }          

Output:

output 3

In the above program, the two headers, iostream, and cassert are included to be able to make use of cin, cout, and assert functions. Then the main method is called, within which an integer variable called num is defined, which stores the input value entered by the user. Then the assert function is called to check if the assumption made by the user that the user enters a value less than or equal to 5 is true or false. If it is true, the program ends normally as shown in the snapshot above otherwise ends with an error message.

Recommended Articles

This is a guide to C++ assert. Here we discuss the concept of assert function in C++ through definition, syntax, working of assert function through programming examples and their outputs. You may also have a look at the following articles to learn more –

  1. C++ reserve()
  2. C++ Formatter
  3. C++ Multiset
  4. C++ find()

hernandeztelly1999.blogspot.com

Source: https://www.educba.com/c-plus-plus-assert/

0 Response to "How to Write Assert and Continue in Main Win32"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel