none
Password checker in small basic RRS feed

  • Question

  • Hi all - new to all lof this.

    I am trying to create a program to check if a correct password has been entered. I have managed to write a program that checks the password and returns correct or incorrect - but only allows the user one attempt.

    I would like it to give the user infinite attempts until correctly entrered.

    This is what I have so far;

    TextWindow.Title = "Password Checker Program"

    TextWindow.WriteLine("Please enter your password")

    TextWindow.WriteLine("")

    Password = TextWindow.Read()

    TextWindow.WriteLine("")

    If Password = "sample" Then
      TextWindow.WriteLine("Correct")
    Else
      TextWindow.WriteLine("Incorrect")
    EndIf

    Sunday, April 1, 2012 1:04 PM

Answers

  • The GoTo will work here, but it is generally not considered good programming to use it too much.

    All the While does is continue looping  (doing the code between the While and EndWhile as long as (or while) the statement in with the While is true).

    So

    While (statement)
    your code
    EndWhile

    'your code' is just what is repeated each time that 'statement' is True.

    statement can be anything that can be evaluated to be True or False:

    We use a while to do a loop (like a for loop), but when we don't know before we start how many times to do it - the number of times often depends on how the user interacts.

    Some examples:

    While ("True")
      'Loops for ever
    EndWhile
    
    x = 0
    While (x < 100)
      x = x+Math.GetRandomNumber(10)
      TextWindow.WriteLine(x)
    EndWhile
    
    start = Clock.ElapsedMilliseconds
    While (Clock.ElapsedMilliseconds < start + 5000)
      'Keeps repeating for 5 sec
    EndWhile

    Or in your case:

    TextWindow.Title = "Password Checker Program"
    Password = ""
    While Password <> "sample"
      TextWindow.WriteLine("Please enter your password")
      TextWindow.WriteLine("")
      Password = TextWindow.Read()
    EndWhile
    TextWindow.WriteLine("Correct")
    

    • Marked as answer by nbssmallbasic Sunday, April 1, 2012 7:03 PM
    Sunday, April 1, 2012 5:36 PM
    Moderator

All replies

  • passwordCorrect = false;
    while (passwordCorrect == false)
        // Your code
        (if correct password) passwordCorrect = true;
    endwhile
    There's some psuedo-code mixed in with actual code.
    Sunday, April 1, 2012 2:17 PM
  • You need to go back and ask again for a password if the input is incorrect - there are several ways this could be done.  The best is using a While .... EndWhile structure.

    Have a look at the While command and get back if you have problems, something along these lines:

    Password = ""
    While Password <> "sample"
      'get a password here
    EndWhile

    Note that if the user forgets the password the program will  go for ever asking (infinite attempts).

    Sunday, April 1, 2012 2:25 PM
    Moderator
  • Thanks for your responses - I had read about the While.... EndWhile structure but couldn't get to grips with it - all the examples contained number variables and I couldn't work out how to apply this to text.

    Whilst awaiting a response I tried the GoTo command and it seems to have worked. Not sure if this is the correct convention for this particular task but will try again with the While EndWhile approach.

    Cheers

    Sunday, April 1, 2012 5:16 PM
  • The GoTo will work here, but it is generally not considered good programming to use it too much.

    All the While does is continue looping  (doing the code between the While and EndWhile as long as (or while) the statement in with the While is true).

    So

    While (statement)
    your code
    EndWhile

    'your code' is just what is repeated each time that 'statement' is True.

    statement can be anything that can be evaluated to be True or False:

    We use a while to do a loop (like a for loop), but when we don't know before we start how many times to do it - the number of times often depends on how the user interacts.

    Some examples:

    While ("True")
      'Loops for ever
    EndWhile
    
    x = 0
    While (x < 100)
      x = x+Math.GetRandomNumber(10)
      TextWindow.WriteLine(x)
    EndWhile
    
    start = Clock.ElapsedMilliseconds
    While (Clock.ElapsedMilliseconds < start + 5000)
      'Keeps repeating for 5 sec
    EndWhile

    Or in your case:

    TextWindow.Title = "Password Checker Program"
    Password = ""
    While Password <> "sample"
      TextWindow.WriteLine("Please enter your password")
      TextWindow.WriteLine("")
      Password = TextWindow.Read()
    EndWhile
    TextWindow.WriteLine("Correct")
    

    • Marked as answer by nbssmallbasic Sunday, April 1, 2012 7:03 PM
    Sunday, April 1, 2012 5:36 PM
    Moderator
  • Thank you very much for your help.

    Didn't realise that the EndWhile would be used on its own - I was trying to incorporate it into the If.

    The supplied code works a treat and looks much more streamlined than my attempt.

    Once again - thanks

    Sunday, April 1, 2012 7:07 PM
  •                                                    

    Heya nbssmallbasic!

    I just wanna tell you that GoTo statement in itself is not a bad word or a sin! I'm a lil' biased due to my nick here.  Tee-hee!
    It's very useful to create loops in which a condition should be checked at its end, opposite of While...EndWhile, where it happens at its beginning.
    Unfortunately, SmallBasic doesn't provide specialized loops like these below:

    Loop                                                       Repeat
    ...                                                             ...
          <code>                                 or               <code>
    ...                                                             ...
    While <condition is still TRUE>                  Until <condition becomes TRUE>

    And no means to neatly jump out of one like the C language break keyword offers. Nor force next iteration as in continue keyword. So we oughta improvise!

    Let's see how to use GoTo keyword to emulate those types of loops above:

    SecretPass = "sample"
    TextWindow.Title = "Password Checker Program"
    TextWindow.WriteLine("Please enter your password")
    
    '/***************************************************************\'
    AskAgain:
    Password = TextWindow.Read()
    TextWindow.WriteLine("")
    
    If Password <> SecretPass Then   ' While they're different
      TextWindow.WriteLine("Incorrect! Try again!!!")
      Goto AskAgain    ' Reapeat loop!!!
    EndIf
    '\***************************************************************/'
    
    'We've escaped loop's grasp from here on ;-P
    TextWindow.WriteLine("Access granted! Welcome!!!")
    TextWindow.WriteLine("")

    We test w/ If...Then weather Password is still not equal to SecretPass. And issue a Goto AskAgain if that's the case. Once that becomes FALSE, it escapes the AskAgain loop!
    It's almost as good as if Loop...While or Repeat...Until were available in SB. (^_^)

    But what if you still wanna get in line w/ the "standard way" of doing things, that is, you wanna use a "formal" While...EndWhile loop? Then we're gonna need a variable to store loop's running state. Let's call it KeepGoing in this example:

    SecretPass = "sample"
    TextWindow.Title = "Password Checker Program"
    TextWindow.WriteLine("Please enter your password")
    
    KeepGoing = "True"
    '/***************************************************************\'
    While KeepGoing
      Password = TextWindow.Read()
      TextWindow.WriteLine("")
    
      If Password = SecretPass Then   ' Until they are equal
        KeepGoing = "False"  ' Tell While...Loop not to get going!
      Else
        TextWindow.WriteLine("Incorrect! Try again!!!")  ' Otherwise, we're still trapped!
      EndIf
    EndWhile
    '\***************************************************************/'
    
    'We've escaped loop's grasp from here on ;-P
    TextWindow.WriteLine("Access granted! Welcome!!!")
    TextWindow.WriteLine("")

    Comparing this one w/ GoTo's approach, you can see we had to invert the If..Then clause from Password <> SecretPass to Password = SecretPass. Which means we are waiting for them to match! When that happens, we flag KeepGoing = "False".
    Since While statement keeps checking up weather KeepGoing is still = "True", once it evaluates as FALSE, While refuses to execute its loop block. As you can see, KeepGoing variable is used as a mean to communicate to While our order to end the loop.

    Another slightly variation is what's litdev already showed you. Instead of While w/ a flag variable condition, we directly use the condition present inside If...Then clause and transfer it into While's! We just need to make sure to initialize a participating variable to result as TRUE before While..EndWhile block starts:

    SecretPass = "sample"
    TextWindow.Title = "Password Checker Program"
    TextWindow.WriteLine("Please enter your password")
    
    Password = ""  ' some value to make 1st While condition TRUE
    '/***************************************************************\'
    While Password <> SecretPass
      Password = TextWindow.Read()
      TextWindow.WriteLine("")
    
      If Password <> SecretPass Then
        TextWindow.WriteLine("Incorrect! Try again!!!")
      EndIf
    EndWhile
    '\***************************************************************/'
    
    'We've escaped loop's grasp from here on ;-P
    TextWindow.WriteLine("Access granted! Welcome!!!")
    TextWindow.WriteLine("")

    If we judge those 3 methods, we'll see that the use of GoTo keyword didn't made code any harder to understand or turned it into "spaghetti". In fact, it was straight to the point and a little bit clearer to figure out what it does!

    Next reply, I'm gonna explain other uses of GoTo for loop control purposes and then, why GoTo is demonized amongst programmers!  >;-)

    Monday, April 2, 2012 4:36 AM
    Answerer