PT1420: Repetition Structures in Visual Basic
Page 1
This lab requires you to write the program in the Visual Basic console application using loops.
Read the following program prior to completing the lab.
Food Incorporated wants you to create a small program that will ask customers to enter their
name, address, city, state, zip code and to validate that they provide the information in order to
complete the program.
Complete the following lab tasks.
Step 1: Start the program
a. Create a new Visual Basic Console Application by going to file New project
b. Name the application lab4.2_fname_lname and click Ok
Step 2: Create the initial code asking for the user’s name
a. In the main function write the following code to request a user to enter their
name and pause the program when finished:
'Create the variables
Dim strFname As String = “”
Console.Write("Enter your first name:")
strFname = Console.ReadLine()
Console.Write("Hello " & strFname & vbCrLf)
Console.Write("Press any key to close")
Console.ReadLine()
PT1420: Repetition Structures in Visual Basic
Page 2
Your module looks like this:
b. Run the program by clicking the green arrow that points to the right or by going
to debug start debugging
Test your program by first entering your name; observe what happens.
Test your program again but this time just press Enter. Note, nothing
stopped you from entering your name. Your output looks like this:
PT1420: Repetition Structures in Visual Basic
Page 3
Step 3: Add a module to collect input
a. Create a new module called collectAndValidateName() and move the input of
the users information to this module. Update your main function to look like the
following:
Sub collectAndValidateName()
'Create the variables
Dim strFname As String = ""
'Create the first validation loop
'Prompt for user to enter name
Console.Write("Enter your first name:")
'Read user input
strFname = Console.ReadLine()
'check if the user entered a value
Console.Write("Hello " & strFname & vbCrLf)
Console.Write("Press any key to close")
Console.ReadLine()
b. Call your collectAndValidateName() module from the main() module. Update
your main function to look like the following:
Sub Main()
PT1420: Repetition Structures in Visual Basic
...
PT1420 Repetition Structures in Visual Basic .docx
1. PT1420: Repetition Structures in Visual Basic
Page 1
This lab requires you to write the program in the Visual Basic
console application using loops.
Read the following program prior to completing the lab.
Food Incorporated wants you to create a small program that will
ask customers to enter their
name, address, city, state, zip code and to validate that they
provide the information in order to
complete the program.
Complete the following lab tasks.
Step 1: Start the program
a. Create a new Visual Basic Console Application by going to
project
b. Name the application lab4.2_fname_lname and click Ok
Step 2: Create the initial code asking for the user’s name
a. In the main function write the following code to request a
user to enter their
2. name and pause the program when finished:
'Create the variables
Dim strFname As String = “”
Console.Write("Enter your first name:")
strFname = Console.ReadLine()
Console.Write("Hello " & strFname & vbCrLf)
Console.Write("Press any key to close")
Console.ReadLine()
PT1420: Repetition Structures in Visual Basic
Page 2
Your module looks like this:
b. Run the program by clicking the green arrow that points to
the right or by going
Test your program by first entering your name; observe what
3. happens.
Test your program again but this time just press Enter. Note,
nothing
stopped you from entering your name. Your output looks like
this:
PT1420: Repetition Structures in Visual Basic
Page 3
Step 3: Add a module to collect input
a. Create a new module called collectAndValidateName() and
move the input of
the users information to this module. Update your main function
to look like the
following:
Sub collectAndValidateName()
'Create the variables
Dim strFname As String = ""
4. 'Create the first validation loop
'Prompt for user to enter name
Console.Write("Enter your first name:")
'Read user input
strFname = Console.ReadLine()
'check if the user entered a value
Console.Write("Hello " & strFname & vbCrLf)
Console.Write("Press any key to close")
Console.ReadLine()
b. Call your collectAndValidateName() module from the main()
module. Update
your main function to look like the following:
Sub Main()
PT1420: Repetition Structures in Visual Basic
Page 4
5. Call collectAndValidateName()
Console.Write("Press any key to close")
Console.ReadLine()
End Sub
Your module looks like this:
Step 4: Add Input Validation using a do while loop
a. We will now add the code to make sure that the user submits
their name before
we continue and say hi to them. Update your collect
AndValidateName module
to look like the following:
'Create the first validation loop
Do While strFname = ""
'Prompt for user to enter name
Console.Write("Enter your first name: ")
'Read user input
PT1420: Repetition Structures in Visual Basic
6. Page 5
strFname = Console.ReadLine()
'check if the user entered a value
If strFname = "" Then
Console.Write("You did not enter a value, try again"
& vbCrLf)
End If
Loop
Console.Write("Hello " & strFname & vbCrLf)
End Sub
b. Run the program by clicking the green arrow that points to
the right or by going
o Test your program by first entering your name, observe what
happens
c. Test your program again but this time just hit enter. Note that
your program now
requires you to enter your name before continuing to say hello
7. to them. Your
output looks like this:
PT1420: Repetition Structures in Visual Basic
Page 6
Step 5: Add additional code to ask the user to enter their
address, city, state and zip code. You
can use any of the looping methods covered in this week’s
material.
a. Using step 3 create additional modules to validate that the
user provides his or
her address, city, state, and zip code. Your module looks like
this:
PT1420: Repetition Structures in Visual Basic
Page 7
b. Using step 4, create loops to validate that the user provides
his or her address,
city, state, and zip code.
8. c. Write out to the screen the information that the user provided.
d. Run the program by clicking the green arrow that points to
the right or by going
to
e. Test you code to make sure you have to provide each piece of
information
before exiting the program. Your final output looks like this:
PT1420: Repetition Structures in Visual Basic
Page 8
PT1420: Repetition Structures in Visual Basic
Page 9
Step 6: Submit the Visual Basic code as a compressed (zipped)
folder using the following steps:
a. Open Windows Explorer --> Start --> All Programs -->
Accessories --> Windows Explorer.
9. Your Windows Explorer might look as follows:
b. In Windows Explorer, navigate to the folder that contains
your project files. Your
Windows Explorer might look as follows:
PT1420: Repetition Structures in Visual Basic
Page 10
(If you don't recall you can check in Visual Studio by opening
your project, right click
module1.vb file and view the properties. Look at the full path
ex.
C:UsersinstructorDocumentsVisual Studio
2010ProjectsmyFirstProgrammyFirstProgramModule1.vb; in
this case navigate to
C:UsersinstructorDocumentsVisual Studio 2010Projects).
Your module properties
might look as follows:
c. Right click on your project folder and choose send to -->
10. compressed folder. This creates
a zip file of all your code. Your Windows Explorer might look
as follows:
PT1420: Repetition Structures in Visual Basic
Page 11
d. Attach the compressed folder you created to your submission.
Your Windows Explorer
might look as follows:
THOUGHT QUESTIONS:
1. Were you able to successfully complete the code to validate
the address, city, state, and
zip code? If not, what went wrong?
2. What types of loops did you try to use in this assignment?
3. What are some ways that you think this program could be
improved?