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)
Show older comments
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
0 Comments
Accepted Answer
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
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
whos('TotalResidentialCost') % now this variable exists
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)
Instead use strings and +, or use character vectors and another method:
disp("total cost is: "+100) % strings use "" and +
disp(sprintf('total cost is: %.2f',100))
disp(['total cost is: ' num2str(100)])
0 Comments
More Answers (0)
See Also
Categories
Find more on Logical in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!