12.4k views
4 votes
Help me with my C++ code (repost because I forgot to include the whole code)! I've tried different options to try to solve this, but nothing I've come up with so far worked.

Instructions: Complete the HonkHorn() function to output the car model.
If the car model is:

Honda Civic

the HonkHorn() function outputs:

The Honda Civic says beep beep!

Code option 1 that FAILED:

void FancyCar::HonkHorn() {
if (car_model == "Honda Civic") {
cout << "The Honda Civic says beep beep!" << endl;
}
}

Code option 2 that FAILED:
void FancyCar::HonkHorn() {
if (car_model == "Honda Civic") {
cout << "The Honda Civic says beep beep!" << endl;
}
else {
cout << "Beep beep!" << endl;
}
}

This is an example of what the failed test output looks like:
Test feedback
HonkHorn() incorrectly outputted: Beep beep!


File -

#include "FancyCar.h"


FancyCar::FancyCar() : miles_driven(5), gallons_of_gas(14.0), miles_per_gallon(24.0), driving_capacity(14.0), car_model("Old Clunker"), engine_on(false) { //Update the constructors to start with the engine off

}


FancyCar::FancyCar(string carMake, double carMpg) : miles_driven(5), gallons_of_gas(14.0), miles_per_gallon(carMpg), driving_capacity(14.0), car_model(carMake), engine_on(false) { //Update the constructors to start with the engine off

}


// Return car model

string FancyCar::GetModel() {

return car_model;

}


// Return miles per gallon (MPG)

double FancyCar::GetMPG() {

return miles_per_gallon;

}


// Return miles on odometer

int FancyCar::CheckOdometer() {

return miles_driven;

}


// Return amount of gas in tank

double FancyCar::CheckGasGauge() {

return gallons_of_gas;

}


// Honk horn

void FancyCar::HonkHorn() {

//type code here

}


// Drive car requested miles but check for enough

// gas and check for positive value

void FancyCar::Drive(int milesToDrive) {

if (engine_on && milesToDrive > 0) { //Update the Drive() function to identify if the car runs out of gas

double maxDistance = miles_per_gallon * gallons_of_gas;

if (milesToDrive <= maxDistance) {

miles_driven += milesToDrive;

gallons_of_gas -= milesToDrive / miles_per_gallon;

} //Update the Drive() function to only update private members if the engine is on, and the engine turns off if the car runs out of gas

else {

miles_driven += maxDistance;

gallons_of_gas = 0.0;

engine_on = false;

}

}

}


// Add gas to tank. Check for positive value.

void FancyCar::AddGas(double amtToAdd) {

if (!engine_on && amtToAdd > 0) { //Update the AddGas() function to only add gas if the engine is off

gallons_of_gas += amtToAdd;

if (gallons_of_gas > driving_capacity) {

gallons_of_gas = driving_capacity;

}

}

}


// Set boolean variable to true

void FancyCar::StartEngine() {

engine_on = true; //Complete the StartEngine() function to set the boolean variable to true

}


// Set boolean variable to false

void FancyCar::StopEngine() {

engine_on = false; //Complete the StopEngine() function to set the boolean variable to false

}

User Malik
by
7.4k points

1 Answer

2 votes

Step-by-step explanation:

if I did not miss anything, I could only see that the variable "car_model" is initiated by the string "Old Clunker".

I see a function to get the value from the variable.

but I see no code to change the value of the variable.

so, it keeps containing "Old Clunker".

in your function you check if the variable contains "Honda Civic". that check fails, because both strings are different, and so you always either miss the print inside the if-statement or go through the "else" branch.

if there is no way to set the variable to "Honda Civic", this behavior will not change.

you need a function that sets/changes the car_model. or you change the initial string from "Old Clunker" to "Honda Civic" in the code.

User Danny Daglas
by
7.5k points