Another Bug: Something Is Still Wrong
In this lesson, you will learn to find a logic error that occurs only in certain situations.
In the previous lesson, What? It Wasn't Supposed To Do That! Finding Logic Errors, you learned how to find and fix an error in logic. In the example code for that lesson, there is still a bug hiding—one that is harder to find because it occurs only in certain situations.
Testing a Program
As a programmer, when it comes to testing your program to see if it behaves as you want it to, you are at a disadvantage. You know how it should work, so it is unlikely that you will make a mistake that could uncover a logic error. A user who is unfamiliar with the program, however, can and will do things you have not thought of.
For example, in a program that calculates miles per hour by dividing the number of miles traveled by the number of hours the journey took, what happens if the user enters zero for either the hours or the miles? Let's try it and see.
To test the program
Open the LogicErrors project that was created in the previous lesson, What? It Wasn't Supposed To Do That! Finding Logic Errors.
If you did not complete or save the previous project, you will need to go back and complete it before you can continue.
Press F5 to run the program. In the first text box, enter 0 (to represent minutes), and in the second text box, enter 5 (to represent miles), and then click Button1.
A message box is displayed with the message "Average speed Infinity".
Keep the project open—in the next procedure, you will learn how to find the logic error.
5 Divided by 0 = Infinity?
In the previous procedure, "Infinity" might not be what you expected, but it is mathematically correct—0 goes into 5 an infinite number of times. That, however, is not the result you would want users of your program to see. Can you think of a way to prevent this?
You might think of adding an error handler, a procedure you learned in the lesson What To Do When Something Goes Wrong: Handling Errors. However, in this case that wouldn't work—the result "Infinity" isn't an error; it's just not what you want.
Since it is probably not useful to show a speed of zero, one way to fix the problem is to test for a value of zero, then warn the user that they must enter a higher number. While you are at it, you might as well prevent the user from entering negative numbers as well, since negative numbers can also produce a false result.
In the next procedure, you will modify the code in the Button1_Click event handler to call the GetMPH function only if the values are greater than zero.
To fix the bug
In the Code Editor, change the code in the Button1_Click event handler as follows:
Dim minutes As Integer = CInt(Textbox1.Text) Dim miles As Double = CDbl(Textbox2.Text) Dim hours As Double = 0 If minutes <= 0 Or miles <= 0 Then MsgBox("Please enter a number greater than zero") Else hours = minutes / 60 MsgBox("Average speed " & GetMPH(miles, hours)) End If
Press F5 to run the program again. In the first text box, enter 0, and in the second text box, enter 5. Then click Button1.
You should see the message box instructing you to enter a number greater than 0. Try testing your program with other combinations of numbers until you are satisfied that the bug is fixed.
In this lesson, you learned how to find and fix a logic error that caused unexpected behavior. In the next lesson, you will learn how to use comments in your code.
Next Lesson: Making Notes in Your Programs: Using Comments