1

I had created a simple password program with matlab. As you know the typical password programs have a simple idea which is if you enter your password incorrectly three times, the program will send you LOCKED! error. so how can I add the property?

while (x ~= 1111)
    if (x == password)
        uiwait( msgbox('Welcome!') );
        break;
    else
        uiwait( errordlg('Try Again!!') );

        X = inputdlg('Please enter your password');
        x = str2double ( X{1,1} );
    end 
end
 uiwait( msgbox('Welcome!') );
end
Joe
  • 11
  • 3
  • 1
    You're pretty close, instead of using `x` in the while loop, use a variable to count. Initialize it as follows `failed_count = 0` and then run the loop of `while (failed_count < required_count_limit)` and increment the `failed_count` as required – Sudheesh Singanamalla Nov 25 '17 at 13:07

2 Answers2

2
wrong = 0;
lock = false;

% Keeping a char type allows you to use different
% types of passwords (i.e. alphanumeric).
password = '1111'; 

% Infinite loop, which allows you to implement full
% control over the iterations.
while (true)    
    pass = inputdlg('Please enter your password:');

    % If the user pushes the Cancel button, he is not inserting a
    % wrong password, so no action should be taken towards locking.
    % If he pushes the Ok button with empty textbox, {''} is
    % returned, which could be wrong.
    if (isempty(pass))
        continue;
    end

    if (strcmp(pass,password))
        uiwait(msgbox('Welcome!'));
        break;
    else
        uiwait(errordlg('Try again!'));
        wrong = wrong + 1;

        if (wrong == 3)
            lock = true;
            break;
        end
    end
end

if (lock)
    uiwait(errordlg('Application locked!'));
    return;
end

% Your logic starts here...
Tommaso Belluzzo
  • 23,232
  • 8
  • 74
  • 98
  • It's okay. but first of all why Cancel button doesn't work? also there's a simple problem with your locked property: if you enter your password incorrectly first of all Try again will show and then Application locked. you know this feature isn't interesting. I want to show ONLY one message and that's Application locked! – Joe Dec 09 '17 at 22:44
  • When the user hits the cancel button, it means he didn't want to insert a password... so, technically speaking, it should not be counted as a wrong password input. Last but not least, if that message feature isn't interesting, just remove it; God gave you 10 fingers, and you need only one of them to remove two lines of code. – Tommaso Belluzzo Dec 09 '17 at 22:47
1

You could put a counter that you iterate within the loop:

locked_flag = false
counter = 0
while (x ~= 1111)
    if (x == password)
        uiwait( msgbox('Welcome!') );
        break;
    else
        uiwait( errordlg('Try Again!!') );
        counter = counter + 1
        if (counter == 3)
            locked_flag = true;
            %show 'locked' dialog of some kind here
            break;
        end

        X = inputdlg('Please enter your password');
        x = str2double ( X{1,1} );
    end
end
%can now check if locked_flag true to start unlock logic or other...

EDIT: Yes I'd only posted a snippet of your code, you need the logic above it, as such, sorry if that wasn't clear:

X = inputdlg ('Please enter your password');
x = str2double (X {1,1} );
password = 1111; %whatever
if (x == password)
    uiwait( msgbox('Welcome!') );
else
    locked_flag = false
    counter = 1
    while (x ~= 1111)
        if (x == password)
            uiwait( msgbox('Welcome!') );
            break;
        else
            uiwait( errordlg('Try Again!!') );
            counter = counter + 1
            if (counter == 3)
                locked_flag = true;
                %show 'locked' dialog of some kind here
                break;
            end

            X = inputdlg('Please enter your password');
            x = str2double ( X{1,1} );
        end
    end
end
%can now check if locked_flag true to start unlock logic or other...
Antoine Zambelli
  • 724
  • 7
  • 19