Stuck in infinite loop?

2 次查看(过去 30 天)
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.

采纳的回答

Image Analyst
Image Analyst 2015-12-7
"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))
  4 个评论
NN
NN 2019-11-20
Hi Sir,
I am using if conditions in simulink with logical operators and i experince same problem , matlab hangs after running the simulation.I doubt if the loop goes to inifinity and if so how can i correct it in simulink .My model is a power system model and if condition has to check net power at ac grid continuosly during simulation time.can i add any other block in simulink itself to solve this isissue or need to write program
Image Analyst
Image Analyst 2019-11-20
Use a failsafe like I showed: a check on the number of iterations so you don't get into an infinite loop.
Attach your code if you need more help. But I don't have Simulink so I can only advise on MATLAB code.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by