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\n";
    }
    else{
        cout<<*sensorPtr;
    }

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

Solving Approach

 

 

 

 

 

 

Upvote
Downvote
Loading...

Expected Output

NO SENSOR