0% found this document useful (0 votes)
20 views2 pages

File Task 1 (Valid Code)

This document contains code to perform the following tasks: 1) Add 10 codes to a text file by prompting the user to enter each code. 2) Display all codes from the text file on the console. 3) Add a new code to the text file by prompting the user to enter a code. 4) Check if a code is valid by validating the code length, characters, and format, and return true or false. 5) Read codes from the text file, check for valid codes using the validation function, count the number of valid codes, and display them along with the count.

Uploaded by

Pushali Bundhoo
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views2 pages

File Task 1 (Valid Code)

This document contains code to perform the following tasks: 1) Add 10 codes to a text file by prompting the user to enter each code. 2) Display all codes from the text file on the console. 3) Add a new code to the text file by prompting the user to enter a code. 4) Check if a code is valid by validating the code length, characters, and format, and return true or false. 5) Read codes from the text file, check for valid codes using the validation function, count the number of valid codes, and display them along with the count.

Uploaded by

Pushali Bundhoo
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

Sub Main()

Call AddTenCodes()
Call displaycodes()
Call addnewcode()

Call displaycodes()
Call FindValidCodes()
End Sub
Sub AddTenCodes()
Dim filename, code As String
Dim count As Integer
filename = "D:\My files\Documents\2023\CODELIST.TXT"
FileOpen(1, filename, OpenMode.Output)
For count = 1 To 10
Console.WriteLine("Enter code : ")
code = Console.ReadLine()
WriteLine(1, code)
Next
FileClose(1)
End Sub
Sub displaycodes()
Dim filename, code As String
filename = "D:\My files\Documents\2023\CODELIST.TXT"
FileOpen(1, filename, OpenMode.Input)
While Not EOF(1)
Input(1, code)
Console.WriteLine(code)
End While
FileClose(1)
Console.ReadLine()
End Sub

Sub addnewcode()
Dim filename, code As String
filename = "D:\My files\Documents\2023\CODELIST.TXT"
FileOpen(1, filename, OpenMode.Append)
Console.WriteLine(" Enter code : ")
code = Console.ReadLine()
WriteLine(1, code)
FileClose(1)

End Sub

Function CheckCodeValid(ByVal code As String) As Boolean


Dim valid As Boolean = True
Dim middle, last As String
Dim i As Integer
middle = Mid(code, 3, 3)
last = Right(code, 1)
If Len(code) <> 6 Then
valid = False
End If
For i = 1 To 2
If Mid(code, i, 1) < "A" And Mid(code, i, 1) > "Z" Then
valid = False

End If

Next
If IsNumeric(middle) = False Then
valid = False
End If
If last <> "#" Then
valid = False

End If

Return (valid)

End Function

Sub FindValidCodes()
Dim filename, code As String
Dim countvalid As Integer = 0
filename = "D:\My files\Documents\2023\CODELIST.TXT"
FileOpen(1, filename, OpenMode.Input)
While Not EOF(1)
Input(1, code)
If CheckCodeValid(code) = True Then
countvalid = countvalid + 1
Console.WriteLine(code)

End If
End While

Console.Write("Number of codes valid is : " & countvalid)


Console.ReadLine()

FileClose(1)
End Sub

End Module

You might also like