abort() Function with Example in C++

C++ abort() function: Here, we are going to learn about the abort() function with example of cstdlib header in C++ programming language.
Submitted by IncludeHelp, on May 26, 2020

C++ abort() function

abort() function is a library function of cstdlib header. It is used to abort the current process. For the abnormal program termination – we can use abort() function.

Syntax of abort() function:

C++11:

    [[noreturn]] void abort() noexcept;

Parameter(s):

  • None

Return value:

It does not return anything.

Example:

    abort();

C++ code to demonstrate the example of abort() function

// C++ code to demonstrate the example of
// abort() function

#include <iostream>
#include <cstdlib>
using namespace std;

// main() section
int main()
{

    float x, y;
    float result;

    while (1) {
        cout << "Input the value of x: ";
        cin >> x;
        cout << "Input the value of y: ";
        cin >> y;
        
        if (y == 0) {
            cout << "Value of Y cannot be 0" << endl;
            abort();
        }
        
        result = (float)x / (float)y;
        cout << x << "/" << y << ": " << x / y << endl;
    }
    
    return 0;
}

Output

Input the value of x: 10
Input the value of y: 2 
10/2: 5 
Input the value of x: 10
Input the value of y: 3 
10/3: 3.33333 
Input the value of x: 5 
Input the value of y: 3 
5/3: 1.66667
Input the value of x: 6 
Input the value of y: 0 
Value of Y cannot be 0
Aborted (core dumped)

Reference: C++ abort() function




Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.