MATLAB: Stuck in infinite loop

if loopinfinite loopstrfinduser inputvolumewhile loop

Probably doing something stupid..
question = input('Enter a question about volume: ','s');
checkExit = 'exit';
exit = strfind(question,checkExit);
while(length(exit)==0)
ifCube = 'cube';
cube = strfind(question,ifCube);
if(length(cube)==0)
display('you did not specify the shape');
%the word cube isn't in the sentence
else
posH = 'height of';
Index = strfind(question, posH);
height = sscanf(question(Index(1) + length(posH):end), '%d', 1);
volCube = height * height * height
end
ifCyl = 'cylinder';
cylinder = strfind(question,ifCyl);
if(length(cylinder)==0)
%the word cylinder isn't in the sentence
else
posH = 'height of';
Index = strfind(question, posH);
height = sscanf(question(Index(1) + length(posH):end), '%d', 1);
posR = 'radius of';
Index2 = strfind(question, posR);
radius = sscanf(question(Index2(1) + length(posR):end), '%d', 1);
volCyl = 2*pi*radius*height
end
ifCone = 'cone';
cone = strfind(question,ifCone);
if(length(cone)==0)
%the word cone is not in the sentence
else
posH = 'height of';
Index = strfind(question, posH);
height = sscanf(question(Index(1) + length(posH):end), '%d', 1);
posR = 'radius of';
Index2 = strfind(question, posR);
radius = sscanf(question(Index2(1) + length(posR):end), '%d', 1);
volCone = pi*((radius)^2)*(height/3)
end
ifSphere = 'sphere';
sphere = strfind(question,ifSphere);
if(length(sphere)==0)
%the word sphere is not in the sentence
else
posR = 'radius of';
Index2 = strfind(question, posR);
radius = sscanf(question(Index2(1) + length(posR):end), '%d', 1);
volSphere = (4/3)*pi*(radius)^3
end
end
I want to keep asking the user to enter a question about volume until the say 'exit'. However, I don't know how to keep myself from getting stuck in while loop, if I set exit = 1 in each of the if statements it breaks the out of the while loop obviously but I want to continuously ask the user questions. I'm stumped, and a point in the right direction would be very helpful.
Thank you.

Best Answer

"exit" is a built in function that terminates a MATLAB program. Don't use that name, obviously, or any other name of any other built-in function.
Put the strfind inside the while test and inside a call to isempty(), so make those two lines into this single line.
while isempty(strfind(question,checkExit))
Related Question