Filter löschen
Filter löschen

Why is my function giving me a string when I should get a structure?

2 Ansichten (letzte 30 Tage)
Kristen O'Mara
Kristen O'Mara am 16 Feb. 2018
Beantwortet: Kristen O'Mara am 16 Feb. 2018
We are asked to write a function 'carGenerator' that takes 5 inputs and uses conditional statements to determine if a car is valid based on these parameters. If it is not, it should return the string 'Invalid car parameters.'. My function works for the example problems given, however it won't work for the random test and is giving back the string 'Invalid car parameters.' for what should be a valid car.
Here are the parameters given:
"Your program should check to see if the car is valid. Invalid car properties include:
-Values less than 0 for any numeric property.
-A value greater than 100 for fuelORchargeif isElectricis true. Batteries cannot be more than 100% charged."
*Note: isElectric is a Boolean variable determining whether the car is electric or fuel burning
fuelORcharge represents how much fuel (in volume) or how much charge the car has
Here is my current code:
function [car] = carGenerator (mass, passengers, isElectric, fuelORcharge, workPerUnit)
%mass = mass of car (kg)
%passengers = number of passengers
%isElectric = car is electric or not
%fuelORcharge = volume of fuel or percent charge (if electric)
% workPerUnit = rate at which fuel or battery life is converted into energy
if (mass <= 0) (passengers <= 0) (fuelORcharge < 0) (workPerUnit < 0)|| (isElectric == 1 && fuelORcharge > 100)
car = 'Invalid car parameters';
else
car.mass = mass;
car.passengers = passengers;
car.isElectric = isElectric;
car.fuelORcharge = fuelORcharge;
car.workPerUnit = workPerUnit;
end

Antworten (3)

Star Strider
Star Strider am 16 Feb. 2018
You told it to!
You might want to create another field:
car.Message = 'Invalid car parameters';
Remember to assign something to it (even if an empty string) for gasoline, Diesel, and dilithium powered cars.

Kristen O'Mara
Kristen O'Mara am 16 Feb. 2018
The thing is I need that string because they give us two examples of a bad car with inputs that will cause the output to be the string. They also give us a good car example which is supposed to get a structure. However, for the random example they give us I'm getting 'Invalid car parameters." and apparently (after I submitted it for grading) it said:
"error: Variable car must be of data type struct. It is currently of type char. Check where the variable is assigned a value."
So basically idk why my function is discounting valid cars as invalid when I followed what the project directions said.

Kristen O'Mara
Kristen O'Mara am 16 Feb. 2018
"data type char" = string and "data type struct" = structure
So the car is being told it's invalid when it is valid and should be giving me a structure for random inputs.

Kategorien

Mehr zu Propulsion and Power Systems finden Sie in Help Center und File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by