Click here to Skip to main content
15,867,594 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
just need this code looking over and making sure it is okay really, some of it doesn't work! Any help?

VB
Module Module1

    Sub Main()

        Dim MenuOption, MonthChoice, SurnameChoice, textstring As String
        Dim setflag As Boolean
        'setflag will be false until a match is made
        'boolean is an expression meanning either true or false


        Console.WriteLine("This program allows you to access contact details from an adress book")
        Console.WriteLine("You can choose to search by surname or date of birth")
        Console.WriteLine("Press the enter key to continue")
        Console.ReadLine()
        Console.WriteLine("1.Search by surname")
        Console.WriteLine("2.Search by Date of Birth (mm) ")

        Do
            Console.WriteLine("Please choose the program you would like to select")
            MenuOption = Console.ReadLine
            If IsNumeric(MenuOption) And Asc(MenuOption) < 52 And Asc(MenuOption) > 48 Then
                Console.WriteLine("This is a valid Case Choice")
            Else
                Console.WriteLine("This isnt a valid choice please try again")
            End If
        Loop Until Asc(MenuOption) < 52 And Asc(MenuOption) > 48



        Select Case MenuOption
            Case 1

                Console.WriteLine("Please enter the surname")
                SurnameChoice = Console.ReadLine

                setflag = False
                'The reason setflag is set as false is because it needs to be empty initially
                FileOpen(1, "Address_Book.txt", OpenMode.Input)
                'The file is loaded into the console (openmode.input).
                '1 is a unique number asssigned to the data file however there maybe >1 file so each is assigned a number
                Do
                    textstring = LineInput(1)
                    If InStr(textstring.ToLower, SurnameChoice.ToLower) <> 0 Then
                        Console.WriteLine("Details: " & textstring)
                        setflag = True
                        'LineInput it a Vb funtion which reads a single line fron the open text file and assigns it to the varial textstring.
                        'Instr looks for the 1st time a character or string appears
                        'Returns an integer which represents the number position that he character or string appears
                        'ToLower converts the keyboard input & the line of data into lower case so it easier to find a match
                        'The whole if statement compares the sting entered by the user to the 1st line of data in the text file
                        'If the 2 integers dont equal 0 (Meaning there is a match) then the variable setglag is set to true
                    End If
                Loop Until EOF(1) Or InStr(textstring.ToLower, SurnameChoice.ToLower) <> 0
                'Eof stands for End Of File, the program loops until either a match is made or the end of the file is reached
                FileClose(1)
                If setflag = False Then
                    Console.WriteLine("No Details were found matching that surname")
                    'If there is a match then the line assigned to textstring is printed
                End If
                Console.ReadLine()

            Case 2

                Console.WriteLine("Please enter the month of the date of birth (mm).")
                MonthChoice = Console.ReadLine


                setflag = False
                FileOpen(1, "Address_Book.txt", OpenMode.Input)
                Do
                    textstring = LineInput(1)
                    If InStr(textstring, MonthChoice) <> 0 Then
                        Console.WriteLine("People: " & textstring)
                        setflag = True
                    End If

                Loop Until EOF(1)
                FileClose(1) '
                If setflag = False Then
                    Console.WriteLine("No details were found matching that birthday month")

                End If
                Console.ReadLine()

        End Select

    End Sub



End Module
Posted
Comments
Ron Beyer 17-Jun-13 14:41pm    
"Some of it doesn't work" mind helping us out and telling us WHAT doesn't work?
Conorthecoder43 17-Jun-13 14:47pm    
That's what i'm trying to find out hmm
Ron Beyer 17-Jun-13 15:25pm    
Ok, let me put this another way, when you run it, what does it do, and what do you really want it to do? Do you bring your car into the shop and tell them "it doesn't work" and they say "what doesn't work" and you say, "the car stupid!"? We need to know what you expect the code to do in order to tell you why it doesn't do that...

1 solution

Without knowing what you are trying to do, and what the errors you can see are, we can't help that much!
But...there are a few bits that spring out as being a bit weak:
VB
Do
    Console.WriteLine("Please choose the program you would like to select")
    MenuOption = Console.ReadLine
    If IsNumeric(MenuOption) And Asc(MenuOption) < 52 And Asc(MenuOption) > 48 Then
        Console.WriteLine("This is a valid Case Choice")
    Else
        Console.WriteLine("This isnt a valid choice please try again")
    End If
Loop Until Asc(MenuOption) < 52 And Asc(MenuOption) > 48
Then you follow this with a Select Case block which checks them all over again!
Why? Why not put the loop round the case statement?
VB
Dim exitApp As Boolean = False
Do
    Console.WriteLine("Please choose the program you would like to select")
    MenuOption = Console.ReadLine()
    Select Case MenuOption
        Case "Q"
            exitApp = True
            Exit Select
        Case "1"
            ...
            Exit Select
        Case "2"
            ...
            Exit Select
        Case Else
            Console.WriteLine("Unknown choice: please try again")
            Exit Select
    End Select
Loop While Not exitApp
Then move the relevant code for each Case into a separate method and it all looks tidier, and easier to understand and improve.
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900