MATLAB Answers

While Loop regarding user input type stuck on infinite else loop

39 views (last 30 days)
Shadi Khansa
Shadi Khansa on 21 Sep 2020 at 9:47
Commented: Shadi Khansa on 21 Sep 2020 at 15:49
I need to check user input to make sure it's a number. The first part of the while loop works if i input a number, but the 'else' part gets stuck on infinite loop if I input something else, and it won't go out even if I input a number.
%Part of a Program to calculate equivalent resistance for series and parallel circuits
n = input('Enter Number of resistors: ')
for count = 1:n
%input resistance values into an array
R(count) = input(['Enter the value of Resistor ' num2str(count) ': ']);
flag = false;
while ~flag
num = isnumeric(R(count)); %check if resistance input is a number
if num == true %This part works and i can get through the whole for loop if I enter numbers correctly
flag = true;
else %This part gets stuck on an infinite loop even if I enter a number
disp('incorrect entry');
R(count) = input(['Enter the value of Resistor ' num2str(count) ': ']);
end
end
end

  2 Comments

Rik
Rik on 21 Sep 2020 at 9:56
What input are you trying? I don't get any infite loop on R2020a.
For readability, you should use the smart indent (in most releases you can select your code and hit ctrl+i), and remove empty lines that don't divide distinct parts of your code. You should also pre-allocate R and write some comments in your code that describe the flow of your program.
Shadi Khansa
Shadi Khansa on 21 Sep 2020 at 10:09
I edited the code to make it more readable. The for loop works as long as I input numbers, but if I input a character or string, then it gets stuck infinitely on the 'else' part even if I enter a number.

Sign in to comment.

Accepted Answer

Rik
Rik on 21 Sep 2020 at 10:23
The data type of R is not changing, so if you create it as a char, it will never be numeric. If you want to use this setup, you need to use a cell array instead. Another method is to use a temporary variable and store that in R once it is valid:
n = input('Enter Number of resistors: ');
R=zeros(1,n);
for count = 1:n
tmp=NaN;%enter loop
while isnan(tmp)
tmp = input(['Enter the value of Resistor ' num2str(count) ': '],'s');
tmp=str2double(tmp);
end
R(count)=tmp;
end

More Answers (1)

Vasishta Bhargava
Vasishta Bhargava on 21 Sep 2020 at 10:01
Edited: Rik on 21 Sep 2020 at 10:13
Use a break statement in if loop as below
if condition
...
else
...
break;
end

  3 Comments

Shadi Khansa
Shadi Khansa on 21 Sep 2020 at 10:25
I added break. When I enter numbers for the resistor values everything works fine, but here is the output I get if I enter something wrong. It requires two inputs for every following count of the for loop.
Enter number of resistors: 2
Enter the value of Resistor 1: 'd'
incorrect entry
Enter the value of Resistor 1: 1
Enter the value of Resistor 2: 2
incorrect entry
Enter the value of Resistor 2:
Vasishta Bhargava
Vasishta Bhargava on 21 Sep 2020 at 10:34
Make the flag as true inside the else statement as
%if true
% code
% end
if condition
...
else
% disp('incorrect entry')
...
flag = true;
break;
end
Bcoz break statement is used in nested loop it comes out of inner loop only. Or place a break statement outside the if loop but within the while loop
Rik
Rik on 21 Sep 2020 at 10:39
Why are you adding that if true to your code block? That used to be the default example if you added code with the old editor, but it was never needed then, and it isn't needed now.
I think you are also misdiagnosing the issue. As I see it, the problem is not with the control flow, it is with the data itself. Can you explain where the flaw is in my reasoning?

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!

Translated by