Click here to Skip to main content
15,210,822 members
Rate this:
Please Sign up or sign in to vote.
See more:
Create a program that allows the user up to 3 attempts to enter the password 'secret'. If the user enters the password correctly in the 3 attempts, a message is displayed that the password is valid. Otherwise, the program displays the message that the password is incorrect.

USING DO...UNTIL LOOP with IF..THEN

THIS MUST BE DONE USING DO..UNTIL LOOP. PLEASE NO IF..THEN.
THE TEACHER ASKED TO FIND THE SOLUTION USING DO..UNTIL LOOP
THIS IS FOR A SCHOOL ASSIGNMENT.

What I have tried:

I was able to do it through just IF..THEN but I cant do it using DO..UNTIL LOOP.
Please its urgent.
This the coding that I have used, but it doesn't work correctly.
Please give me the correct answer.
Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim pass, word As String
        Dim num As Integer
        num = 1
        pass = "secret"
        word = TextBox1.Text
        'If word = pass Then
        '    MsgBox("VALID")
        'Else
        '    MsgBox("INVALID")
        'End If
        'num = num + 1
        'Do
        '    num = num + 1
        'Loop Until num = 3
        'MsgBox("RUN")
        Do
            If num >= 3 Then
                MsgBox("ATTEMPTS EXCEEDED")
            End If
            If word = pass And num <= 3 Then
                MsgBox("VALID")
            End If
            'Do
            '    num += 1
            'Loop Until num > 3
            'If num >= 3 Then
            '    MsgBox("ATTEMPTS EXCEEDED")
            'End If
            'If word <> pass Then
            '    MsgBox("INVALID")
            'End If
            num += 1
            If word <> pass Then
                MsgBox("INVALID")
            End If
        Loop Until num > 3
    End Sub
End Class
Posted
Updated 3-Nov-16 0:09am
v4
Comments
[no name] 28-Oct-16 8:06am
   
As far as i know WinForms, the code sample you updated will fire at the first Button Click and if correct it will procede otherwise the attemps condition will have no effect. But you can still declare the 'num' variable as STATIC, it will make some sense.
[no name] 28-Oct-16 8:51am
   
Using a Do loop in this manner in an event driven program makes no sense.
Rate this:
Please Sign up or sign in to vote.

Solution 1

You dont need a Do Loop for this unless you are doing other calculations.

At your Form or UserControl, where the Button of Login is present add;

Dim attempts As Integer = 0

Dim userName As String = "user123"
Dim password As String = "12345"

Private Sub btLogin_Click(sender As Object, e As EventArgs) Handles btLogin_Click

If attempts >=3 Then
'maximal attempts reached
MsgBox("You reached the maximum attempts to log in!")
Exit Sub
End If


If (textBoxUserName.Text = userName) AndAlso (textBoxPasword.Text = password) Then
'Correct username and password

   'add code to procede

Else
'Incorrect username or password
attempts +=1
End if


End Sub
   
Comments
Ralf Meier 28-Oct-16 3:52am
   
I don't know why this answer was downvoted. In my opinion it matches completly to the question ... (I upvoted it to correct this)
But I think, the OP asked for a Solution based on Console-Operations (because it is a kind of Homework). With Console-Operations it is possible to do this with a Loop - in an Event-based Application that isn't possible.

Perhaps the OP should provide his code ...
[no name] 28-Oct-16 8:35am
   
I would guess that is was downvoted because the question (and probably homework assignment) clearly states the the OP must use a Do loop. So , this solution does not answer the question as asked.
Infinitium2002 28-Oct-16 6:30am
   
Here this question should be done using The Do LOOP.
Ralf Meier 30-Oct-16 10:33am
   
In this case Solution 3 should nearly match to your requirement.
But in future : If you want a Solution which matches to your requirement then tell us all the conditions and ... perhaps provide some code. Downvoting an answer in this condition for me is stupid ...
Rate this:
Please Sign up or sign in to vote.

Solution 2

Try something like this:
Dim attempts As Integer
attempts = 0
Do
    attempts = attempts + 1
    If password = "secret" Then
        MsgBox("VALID")
        Exit Do
    Else
        MsgBox("Invalid")
    End If
Loop Until attempts > 2
   
Comments
Infinitium2002 28-Oct-16 21:49pm
   
This didnt work.
[no name] 29-Oct-16 4:54am
   
And it's not going to in an event driven program. You have already been told this.
Rate this:
Please Sign up or sign in to vote.

Solution 3

Here it is as a console app without any if-thens.

Remember you can put multiple conditions on the loop exit.

Dim pwTries As Integer = 0
Const secretPassword = "secret"
Dim pwMatch As Boolean = False
Do Until pwMatch Or pwTries > 2
    Console.WriteLine("Enter you password.")
    pwMatch = (Console.ReadLine = secretPassword)
    pwTries += 1
Loop
Console.WriteLine("Password is " & If(pwMatch, "valid", "incorrect") & ".")
Console.WriteLine("Click any key to continue.")
Console.ReadKey()
   
Comments
Infinitium2002 30-Oct-16 21:16pm
   
Your Programme is ok. But I should do it in a windows forms application. Should use the do form as
Do
statement(s)
loop until condition
Rate this:
Please Sign up or sign in to vote.

Solution 4

Here, I found the Solution. Here it is,
dim pass,user as string
dim attempt as integer=0
pass="secret"
do
attempt+=1
user=InputBox("ENTER PASSWORD. THIS IS ATTEMPT " & attempt)
loop until attempt=3 or user=pass
If user=pass then
msgbox("VALID")
else
msgbox("INVALID")
EndIf
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100