Safe Sensor nullptr Check

#include <iostream>
using namespace std;

int main() {
    int flag;
    cin >> flag;
    int *sensor = nullptr;

    if(flag == 1){
        sensor = new int;
        cin >> *sensor;
    }

    if(sensor != nullptr){
        cout << *sensor;
    }
    else{
        cout << "NO SENSOR";
    }

    delete sensor;

    return 0;
}

Solving Approach

 

 

 

 

 

 

Upvote
Downvote
Loading...

Input

0

Expected Output

NO SENSOR