If-elseif-else-end structure doesn't work properly

3 views (last 30 days)
Hello, I am having some issues with a volume calculating program I am trying to create. For whatever reason, after a volume is calculated, the program does not end, it jumps to the next solid to calculate its volume. For example, if I have just calculated the volume of a sphere, it will then go to the cube function and begin asking for inputs related to that function. I don't understand why it just doesn't end. When the functions are run separately, they end fine. But when ran through the 'main' program (the piece of code below), the issue I mentioned pops up. Any suggestions?
solid = input('Welcome to Volume Calculator - Please enter the solid for which you would like to calculate the volume: ');
if solid == cube
volume = cube(a)
elseif solid == sphere
volume = sphere(r)
elseif solid == prism
volume = prism(a,b,c)
else disp('Only certain solids can be calculated. Please try again')
end

Accepted Answer

Walter Roberson
Walter Roberson on 23 Nov 2011
Consider your lines
if solid == cube
volume = cube(a)
The second of those lines shows us that "cube" is a function. In the "volume =" line, the function is invoked with one argument.
Now look again at the first of those lines. "cube" appears there, and we know that "cube" is a function, so when "cube" occurs there, that means to invoke the function "cube" with no arguments, just as if you had written
if solid == cube()
Then whatever is returned from the cube() function will be compared to the value you input for "solid". As those values probably do not match, the "elseif" branch will be invoked, but the code there has the same issue...
I predict that the corrected code would be
solid = input('Welcome to Volume Calculator - Please enter the solid for which you would like to calculate the volume: ', 's');
if strcmpi(solid,'cube')
volume = cube(a)
elseif strcmpi(solid, 'sphere')
volume = sphere(r)
elseif strcmpi(solid, 'prism')
volume = prism(a,b,c)
else disp('Only certain solids can be calculated. Please try again')
end

More Answers (1)

lbon jbn
lbon jbn on 24 Nov 2011
What you guys said worked except for the fact that I had to remove the input arguments for the functions so rather than prism (a,b,c) it was just prism (it was giving an error when I had the input arguments in).
Thank you both for your help...I was also just wondering though, if I want the program give an error message when someone types in a negative value for one of the input arguments, how would that work? I have done it for a scalar but there is an option to enter a vector (so a range of values) - and I'm not sure how to make the if statement identify whether or not a single element out of the entire array is negative.This is what I have for a scalar and it works fine:
if r < 0
disp ('r cannot be a negative value. Please try again.')
else volume = (4/3)*pi.*r.^3 ;
end

Categories

Find more on Spline Postprocessing in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!