Challenge

Project 2 - Implement the Code for controlling the retraction and extension of Airplane’s landing gear

#include <stdio.h>
#include <stdbool.h>
#include <unistd.h>


int main()
{
// Use an enum to define the possible states of the valve
typedef enum {
VALVE_CLOSED,
VALVE_OPEN
} ValveState;

ValveState valveState = VALVE_CLOSED; // Start with the valve closed

bool done = false; // Use a boolean flag to control the loop

while (!done) { // Loop until done is set to true
int input; // Declare the input variable

printf("Enter 0 to close the valve, 1 to open it, or 9 to quit: ");
scanf("%d", &input); // Read the user input

switch (input) {
case 0:
if (valveState == VALVE_CLOSED) {
printf("Valve is already closed.\n");
} else {
printf("Closing valve.\n");
sleep(2);
valveState = VALVE_CLOSED;
printf("Valve is now closed.\n");
}
break;

case 1:
if (valveState == VALVE_OPEN) {
printf("Valve is already open.\n");
} else {
printf("Opening valve.\n");
sleep(2);
valveState = VALVE_OPEN;
printf("Valve is now open.\n");
}
break;

case 9:
printf("Quitting.\n");
done = true; // Set the flag to exit the loop
break;

default:
printf("Invalid input. Please try again.\n");
break;
}
}

return 0;
}

Your portfolio, next

Ship work like this
in your own course.

Talk to a career counsellor

Free 20-minute call. We'll map the right course to where you want to work.

By submitting you agree to our Terms and Privacy Policy.