Visual Basic
Assignment #6
This page has been visited
times since May 10, 1999.
What new topics are covered in this assignment?
About Loops
Loops execute the same code over and over until a certain condition is met. In the case of the do until loop, the code between the Do and the Loop statements is the code that will be executed repetitively until the condition after the Until statement is met.
For example. In the code below the numbers 1 to 10 are printed in a label. This is because the code between the Do and Loop instructions are done repetitively until x is greater than 10. Notice that I add 1 to the value of x each time the loop is executed. This makes sure that the loop will eventually end and I will not have an infinite loop.
x = 1
Do Until x > 10
label1.caption = label1.caption + cstr(x) + chr(13)
x = x + 1
Loop
As you will see, loops have many uses in computer programming. One application is to verify user input. After the user input is read, the loop checks for errors in it. If an error is found, the loop prints an error message and then reads another value. This process continues until the input is valid.
It is this type of application we will use in this assignment.
Description of Assignment #6
This assignment will read in two numeric values using inputboxes. The program then calculates the product, difference, sum, and exponent of the two numbers and prints the answer double spaced in a label on the screen.
In previous programs, we terminated the program if a non-numeric value was input when a numeric value was expected. In this program we will use the do until statement to read another value if the value is non-numeric. This means that we can continue the program even with invalid input. This is very important in long programs where you would not want to start over just because you made a small input error.
We still want to terminate the program if the input value is blank indicating that there was no input at all or that the user clicked cancel.
Below is the code that performs this type of check. All text after the ' sign is considered comments and is ignored by Visual Basic. I show the comments in yellow below:
Dim intxt as String
Dim x as Single
*********************
intxt = InputBox("Enter a value:",
"Enter value") '
read a value into a string
If intxt = "" Then ' if the user clicks cancel or does not
enter data exit the procedure
Exit Sub
End If
' This loop continues to
execute until intxt is a numeric value or until Cancel is
clicked.
' Each time a non-numeric value is entered, an error message is
printed and another value is read.
' If cancel is clicked or no data is entered, the procedure is
terminated
Do Until IsNumeric(intxt)
ans = MsgBox("Value must be numeric", vbOKOnly +
vbCritical, "Invalid Data")
intxt = InputBox("Enter a value:", "Enter
value")
If intxt = "" Then
Exit Sub
End If
Loop
x = Val(intxt) ' Convert
the string called intxt to a number and store it in variable x
************************
The method for completing this assignment is as follows:
As always, you should read the pertinent parts of the textbook before doing the assignment. If you have any questions please email me. The executable of this assignment can be downloaded below.
Note: Test my program with all the possible input values to see how it works, so that you can make yours the same.
Good luck.
Below are the assignment 6 downloads:
If you have Visual Basic 6 installed on your computer, download the Assignment #6 file only.
If you do not have Visual Basic 6 installed on your computer, you will need the Visual Basic 6 .dll files in addition to the assignment #6 file. The .dll download below is the same one used for assignments 1 to 5, so if you already downloaded it you will not need to download it again. Simply use the same files. Otherwise download both the Assignment #6 file and the Assignment 6 required .dll files below. To run, place all files in the same directory and double-click on math.exe.
Assignment #6 file (5,000 bytes)
Assignment 6 required .dll files (1.3 million bytes)
To e-mail me click here
Page created and maintained by Ron Patterson