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;
    }

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

    delete sensorPtr;   // safe even when sensorPtr == nullptr
    return 0;
}
Upvote
Downvote
Loading...

Input

0

Expected Output

NO SENSOR