Question: I need help with my C + + program. Program below is a a class called MotorStatus and you may only modify the turnOff function

I need help with my C++ program. Program below is a a class called MotorStatus and you may only modify the turnOff function and main function.
- The turnOff function should turn off the guessed motor (if it is over heating) and return true if the correct motor/s are turned off. (not working)
- If the motor is not overheating it leave it in is original state on/off, respectively.(not working)
- If the user guesses wrong the overheating motor will get so hot another motor will overheat. (working -updateOverheating())
)
I need help with how to debug the code and fix turnOff to correctly shut down the correct motors according to the user input.The program is listed below:
#include
#include
#include
#include
class MotorStatus {
private:
uint8_t motorState; // Each bit represents the status of a motor (on/off)
uint8_t overheatingMotors; // Motors that are overheating
int turnCount;
// Function to randomly initialize motors (on/off) and overheat one motor
void initialize(){
srand(time(0));
motorState = rand()%256; // Randomly turn motors on/off
updateOverheating(); // Randomly overheat one motor
turnCount =0;
displayStatus();
}
// Incrementally overheat motors with each turn, ensuring the motor is ON and
not already overheating
void updateOverheating(){
int newMotor;
do {
newMotor = rand()%8; // Randomly select a motor
} while (((1<< newMotor) & motorState)==0||// Ensure the motor is ON
((1<< newMotor) & overheatingMotors)!=0); // Ensure the motor
is not already overheating
overheatingMotors |=(1<< newMotor); // Set the selected motor to
overheat
turnCount++;
}
void displayStatus(){
std::cout << "Motor State (ON/OFF): "<< std::bitset<8>(motorState)<<
std::endl;
std::cout << "Overheating Motors: "<< std::bitset<8>(overheatingMotors)
<< std::endl;
std::cout << "Turn Count: "<< turnCount << std::endl;
}
public:
MotorStatus(){
initialize();
}
// Simulate a guess attempt
bool turnOff(uint8_t guess){
// turn off the motors that are associated with the guess
motorState ^=(overheatingMotors & guess);
if (guess == motorState){
}
// Motors that are not overheating and were turned off are restored to ON
updateOverheating(); // Each incorect guess makes another motor overheat
displayStatus();
return false;
}
// Display current motor status (for debugging purposes)
};
int main(){
MotorStatus motorStatus;
uint8_t guess =0b00000001; // Start with all motors off
// Loop until all motors are turned off
while (!motorStatus.turnOff(guess)){
// The student can use bitwise AND/OR/XOR here to adjust the guess based on
the feedback
}
}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!