Stuck in infinite loop?

4 views (last 30 days)
Scott J
Scott J on 6 Dec 2015
Commented: Image Analyst on 20 Nov 2019
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.

Accepted Answer

Image Analyst
Image Analyst on 7 Dec 2015
"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 Comments
NN
NN on 20 Nov 2019
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 on 20 Nov 2019
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.

Sign in to comment.

More Answers (0)

Categories

Find more on Programming 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!