problem executing while loop

2 ビュー (過去 30 日間)
Matlab_Novice
Matlab_Novice 2018 年 2 月 4 日
編集済み: Matlab_Novice 2018 年 2 月 4 日
Can someone please tell me why this code is not working?
clear;
disp('Password should contain at least an uppercase and a lowercase letter, and a digit.');
pwd=input('Please enter password of 6 or more characters here: ','s');
l=length(pwd); % Length of the password is at least 6 characters
%using isstrprop function to check for uppercase & lowercase letters, numbers
% Password has at least one digit - ascii codes 48-57
numsum=sum(isstrprop(pwd,'digit'));
%Password has at least one uppercase letter - ascii codes 65-90
upsum=sum(isstrprop(pwd,'upper'));
%Password has at least one lowercase letter - ascii codes 97-122
losum=sum(isstrprop(pwd,'lower'));
while l<6
pwd=input('Password is shorter than 6 letters. Try again: ','s');
l=length(pwd);
if l>=6
if sum(isstrprop(pwd,'digit'))<1
clear pwd;
pwd=input('You forgot to include at least one digit. Try again: ','s');
l=length(pwd);
elseif sum(isstrprop(pwd,'upper'))<1
clear pwd;
pwd=input('You forgot to include at least one upper case letter. Try again: ','s');
l=length(pwd);
elseif sum(isstrprop(pwd,'lower'))<1
clear pwd;
pwd=input('You forgot to include at least one lower case letter. Try again: ','s');
l=length(pwd);
% If user enters valid password, say "Ok, password valid"
else
% Password has at least one digit - ascii codes 48-57
numsum=sum(isstrprop(pwd,'digit'));
%Password has at least one uppercase letter - ascii codes 65-90
upsum=sum(isstrprop(pwd,'upper'));
%Password has at least one lowercase letter - ascii codes 97-122
losum=sum(isstrprop(pwd,'lower'));
if l>=6 && numsum>0 && upsum>0 && losum>0
disp('Ok, password valid');
end
end
end
end
  2 件のコメント
Greg
Greg 2018 年 2 月 4 日
While you may no longer need your question and answer, other new users may have the same question. They would benefit from being able to read it. Not to mention my answer is now meaningless.
Matlab_Novice
Matlab_Novice 2018 年 2 月 4 日
ok, I'll re-post my question.

サインインしてコメントする。

回答 (1 件)

Greg
Greg 2018 年 2 月 4 日
編集済み: Greg 2018 年 2 月 4 日
Edit - Sorry, said that wrong.
You aren't updating your checks inside the loop. The value of numsum is always the value of the first entered password.

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by