Safe Sensor nullptr Check

#include <iostream>
using namespace std;

int main() {
    int flag;
    cin >> flag;

    int* sensorPtr = nullptr;   // optional sensor pointer

    if (flag == 1) {
        sensorPtr = new int;
        cin >> *sensorPtr;
    }
    if(sensorPtr == nullptr) cout << "NO SENSOR";
    else{
        cout << *sensorPtr;
    }
    // Write your nullptr check and output logic here

    delete sensorPtr;   // safe even when sensorPtr == nullptr
    return 0;
}

Solving Approach

 

 

 

 

 

 

Upvote
Downvote
Loading...

Input

0

Expected Output

NO SENSOR