I am trying to calculate overall cost but it keeps ending on the starting line 'overallcost' and not running im not sure what im doing wrong.

1 view (last 30 days)
It runs asking the user to input cost and earlier in my code it works out the areas of each space. I tried to make it say the total costs for each type of space but it doesnt load up. I then also tried to load the overall cost but it wont load past the individual space costs. (Sorry its long)
choice9 = questdlg('Would you like to calculate costs for spaces?','question','yes','no','yes');
switch choice9
case 'no'
disp('not calculating cost')
case 'yes'
ResidentialCost=input('Please enter the building cost for residential spaces per m^2: ');
OfficeCost=input('Please enter the building cost for office spaces per m^2: ');
EducationalCost=input('Please enter the building cost for educational spaces per m^2: ');
ToiletCost=input('Please enter the building cost for toilet spaces per m^2: ');
StorageCost=input('Please enter the building cost for Storage spaces per m^2: ');
if ResidentialArea<=0 || ResidentialCost<=0
disp('No cost for residential spaces')
else
TotalResidentialCost= ResidentialCost*ResidentialArea;
disp('Your total cost for residential spaces is:'+TotalResidentialCost)
end
if OfficeArea<=0 || OfficeCost<=0
disp('No cost for office spaces')
else
TotalOfficeCost= OfficeCost*OfficeArea;
disp('Your total cost for office spaces is:'+TotalOfficeCost)
end
if EducationalArea<=0 || EducationalCost<=0
disp('No cost for educational spaces')
else
TotalEducationalCost= EducationalCost*EducationalArea;
disp('Your total cost for educational spaces is:'+TotalEducationalCost)
end
if ToiletArea<=0 || ToiletCost<=0
disp('No cost for educational spaces')
else
TotalToiletCost= ToiletCost*ToiletArea;
disp('Your total cost for toilet spaces is:'+TotalToiletCost)
end
if StorageArea<=0 || StorageCost<=0
disp('No cost for storage spaces:')
else
TotalStorageCost= StorageCost*StorageArea;
disp('Your total cost for storage spaces is:'+TotalStorageCost)
end
OverallCost=TotalStorageCost+TotalToiletCost+TotalEducationalCost+TotalOfficeCost+TotalResidentialCost+0;
disp('Your running total is: '+OverallCost)
end
end

Accepted Answer

Voss
Voss on 4 May 2022
It may be that one or more of TotalResidentialCost and the other "total cost" variables are undefined, because the corresponding area is <= 0 or the inputted cost is <= 0:
ResidentialArea = 0; % suppose the user entered 0 for this
if ResidentialArea<=0 || ResidentialCost<=0
disp('No cost for residential spaces')
else
TotalResidentialCost= ResidentialCost*ResidentialArea;
disp('Your total cost for residential spaces is:'+TotalResidentialCost)
end
No cost for residential spaces
whos('TotalResidentialCost') % then no variable with this name exists here
So you'll get an error when trying to add up all the total costs later.
TotalResidentialCost and the other "total cost" variables have to be defined, even if they're going to be 0:
if ResidentialArea<=0 || ResidentialCost<=0
TotalResidentialCost = 0;
disp('No cost for residential spaces')
else
TotalResidentialCost= ResidentialCost*ResidentialArea;
disp('Your total cost for residential spaces is:'+TotalResidentialCost)
end
No cost for residential spaces
whos('TotalResidentialCost') % now this variable exists
Name Size Bytes Class Attributes TotalResidentialCost 1x1 8 double
Another thing you'll want to fix is: don't use + with character vectors because your output will look like this:
disp('total cost is: '+100)
216 211 216 197 208 132 199 211 215 216 132 205 215 158 132
Instead use strings and +, or use character vectors and another method:
disp("total cost is: "+100) % strings use "" and +
total cost is: 100
disp(sprintf('total cost is: %.2f',100))
total cost is: 100.00
disp(['total cost is: ' num2str(100)])
total cost is: 100

More Answers (0)

Tags

Products


Release

R2022a

Community Treasure Hunt

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

Start Hunting!