Stuck in infinite loop?

조회 수: 2 (최근 30일)
Scott J
Scott J 2015년 12월 6일
댓글: Image Analyst 2019년 11월 20일
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