Home > database >  How do i make my code start from the begging without the user having to restart the program, Visual
How do i make my code start from the begging without the user having to restart the program, Visual

Time:10-16

Here is my code

Module vbModule
    Sub Main()
        'Welcome Text
        Console.WriteLine("Welcome To Squidwards Password Checker")

        'Length Check
        'Creating The Variables
        Dim userPassword As Integer
        Dim passwordLength As String
        'Asking The User To Enter A Password
        Console.WriteLine("Enter A Password That Consists Of 8 Or More Characters")
        userPassword = Console.ReadLine()
        passwordLength = Convert.ToString(userPassword)
        'Repeats Your Password To You, And Tells You It's Length
        Console.WriteLine("You Entered: "   passwordLength)
        Console.WriteLine(passwordLength.Length())
        'Checks If Your Password Is Long Enough
        If passwordLength >= 8 Then
            Console.WriteLine("Your Password Was Long Enough")
        Else
            Console.WriteLine("Your Password Was Too Short")
        End If

        Console.WriteLine("Please Complete The Next Verification Step")

        'Next Verification Test

        'Range Check 
        Dim num As Integer
        'Asks The User To Input A Number Greater Than 6
        Console.WriteLine("Please Write A Number Greater Than 6")
        'Checks If The User Inputted A Number Greater Than 6
        num = Console.ReadLine()
        If num >= 7 Then
            Console.WriteLine("Human Verification Accepted.")
            'If They Dont Input A Number Greater Then 6 They Are Promted With This Statement
        Else
            Console.WriteLine("You Did Not Input A Number Greater Then 6, Please Try Again.")
        End If

        'Peresence Check
        Dim blankData As String
        Dim blankData2 As String
        Console.WriteLine("Please Input Some Characters")
        blankData = Console.ReadLine()
        'Checks if user inputed something or nothing, if they dont they have to restart the verification proccess
        If blankData = "" Then
            Console.WriteLine("You Did Not Input Anything, Please Repeat The Verification Proccess.")

        Else
            Console.WriteLine("Inputted Characters Confirmed")

        End If

    End Sub
End Module

When they fail any verification I want the program to say, press enter to restart, and it brings them back to the beginning

CodePudding user response:

Using the Continue statement, you can skip over the execution of code below the Continue and restart the loop. Additionally, using the Exit statement you can leave the loop when satisfied.

Sub Main()
    While True
        'Welcome Text
        Console.WriteLine("Welcome To Squidwards Password Checker")

        'Asking The User To Enter A Password
        If Not ValidatePassword() Then Continue 'If check failed, restart the loop

        Console.WriteLine("Please Complete The Next Verification Step")

        'Range Check 
        If Not ValidateRange() Then Continue 'If check failed, restart the loop

        'Peresence Check
        If PresenceCheck() Then Exit While 'If check passed, exit the loop
    End While
End Sub

I split all you validation checks into Boolean Functions (return true/false) based on if the validation passed or failed.

An example function would look like

Private Function ValidatePassword() As Boolean
    Console.WriteLine("Enter A Password That Consists Of 8 Or More Characters")
    Dim userPassword As String = Console.ReadLine()
    Dim passwordLength As Integer = userPassword.Length()
    
    'Repeats Your Password To You, And Tells You It's Length
    Console.WriteLine("You Entered: "   userPassword)
    Console.WriteLine(passwordLength)
    
    'Checks If Your Password Is Long Enough
    If passwordLength >= 8 Then
        Console.WriteLine("Your Password Was Long Enough")
        Return True
    Else
        Console.WriteLine("Your Password Was Too Short")
        Return False
    End If
End Function

The checks are the same as you had originally with the inclusion of a Return statement indicating success:true or failure:false

CodePudding user response:

You could wrap your code inside this Do ... Loop While and Try ... Catch, checking for a certain Exception type which you throw when validation fails.

Dim restart = False
Do
    Try
        restart = False
        ' your code in here with failed validation throwing exception like so
        'Welcome Text
        Console.WriteLine("Welcome To Squidwards Password Checker")
        'Length Check
        'Creating The Variables
        Dim userPassword As Integer
        Dim passwordLength As String
        'Asking The User To Enter A Password
        Console.WriteLine("Enter A Password That Consists Of 8 Or More Characters")
        userPassword = Console.ReadLine()
        passwordLength = Convert.ToString(userPassword)
        'Repeats Your Password To You, And Tells You It's Length
        Console.WriteLine("You Entered: "   passwordLength)
        Console.WriteLine(passwordLength.Length())
        'Checks If Your Password Is Long Enough
        If passwordLength >= 8 Then
            Console.WriteLine("Your Password Was Long Enough")
        Else
            Console.WriteLine("Your Password Was Too Short")
            Throw New ArgumentException()
        End If
        ' rest of your code with ArgumentExceptions thrown when validation fails...
    Catch ex As ArgumentException
        restart = True
    End Try
Loop While restart
  • Related