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.

 Respuesta aceptada

Image Analyst
Image Analyst el 7 de Dic. de 2015

0 votos

"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 comentarios

Scott J
Scott J el 7 de Dic. de 2015
Editada: Scott J el 7 de Dic. de 2015
Thank you a lot, I'll start doing that now. What I did to "fix" it before you commented is that I put question into the cube if statement, but then that really only fixed the problem if the user starts with a question about a volume of a cube with height = x. If they ask about any other shapes, it just displays their question until they ask about a cube and then works correctly. How can I fix that?
while(length(exit)==0) %while isempty(strfind(question,checkExit)==0)
ifCube = 'cube';
cube = strfind(question,ifCube);
if(length(cube)==0)
%the word cube isn't in the sentence
question = input('Enter a question about volume: ','s');
%repeats question
checkExit = 'exit';
exit = strfind(question,checkExit);
else
posH = 'height of';
Index = strfind(question, posH);
height = sscanf(question(Index(1) + length(posH):end), '%d', 1);
volCube = height * height * height
question = input('Enter a question about volume: ','s');
%repeats question
checkExit = 'exit';
exit = strfind(question,checkExit);
end
Image Analyst
Image Analyst el 7 de Dic. de 2015
Try it like this instead:
% Ask user for two responses.
defaultValue = {'cube', '42'};
titleBar = 'Enter values';
userPrompt = {'Enter question about volume : ', 'Enter height: '};
loopCounter = 1;
maxNumberOfLoops = 8; % Failsafe - whatever you want.
while loopCounter < maxNumberOfLoops
caUserInput = inputdlg(userPrompt, titleBar, 1, defaultValue);
if isempty(caUserInput),break,end; % Bail out if they clicked Cancel.
% Convert to floating point from string.
usersValue1 = caUserInput{1}
% Bail out if they typed quit or exit also.
if strcmpi(usersValue1, 'quit') || strcmpi(usersValue1, 'exit')
break;
end
usersValue2 = str2double(caUserInput{2})
% Check for a valid number.
if isnan(usersValue2)
% They didn't enter a number.
% They clicked Cancel, or entered a character, symbols, or something else not allowed.
% Convert the default from a string and stick that into usersValue1.
usersValue1 = str2double(defaultValue{2});
message = sprintf('I said it had to be a number.\nI will use %.2f and continue.', usersValue1);
uiwait(warndlg(message));
end
height = usersValue2;
% Now computer volume
volCube = height * height * height;
% Tell user what it is:
promptMessage = sprintf('The volume = %f', volCube);
titleBarCaption = 'Continue?';
buttonText = questdlg(promptMessage, titleBarCaption, 'Continue', 'Quit', 'Continue');
if strcmpi(buttonText, 'Quit')
break;
end
end
NN
NN el 20 de Nov. de 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 el 20 de Nov. de 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.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Loops and Conditional Statements en Centro de ayuda y File Exchange.

Preguntada:

el 6 de Dic. de 2015

Comentada:

el 20 de Nov. de 2019

Community Treasure Hunt

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

Start Hunting!

Translated by