Click here to Skip to main content
14,271,512 members
Rate this:
Please Sign up or sign in to vote.
See more:
Visual basic problem, I need to calculate average and put the output in listbox but i'm having an error?
1. Write a program to compute semester averages. Each set of five lines in a text file should contain a student's social security number and the grades for three hourly exams and the final exam. (The final exam counts as two hourly exams.) the program should display each student's Social Security number and semester average, and then the class average. Use the data in Table : Students grades:[^]

and this is what i did:
Public Class Form1

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim sr As IO.StreamReader = IO.File.OpenText("AVERAGE.TXT")
Dim SSN, Exam1, exam2, exam3, finalexam, average As Double
SSN = CDbl(sr.ReadLine)
Exam1 = CDbl(sr.ReadLine)
exam2 = CDbl(sr.ReadLine)
exam3 = CDbl(sr.ReadLine)
finalexam = CDbl(sr.ReadLine)
average = CDbl((Exam1 + exam2 + exam3 + finalexam + finalexam) / 5)
Dim fmtstr As String = "{0,-13} {1,6}"
With ListBox1.Items
.Add(String.Format(fmtstr, "Soc. Sec. No.", "Average"))
.Add(String.Format(fmtstr, SSN, average))
.Add(String.Format(fmtstr, SSN, average))
.Add(String.Format(fmtstr, SSN, average))
End With
End Sub
End Class

but every time i click on the button it's ending the program :(
please i need your help
Updated 9-Apr-11 5:00am
Wendelius 9-Apr-11 11:04am
D7000 9-Apr-11 11:06am
yes :(
I tried to solve it but c what i got!
OriginalGriff 9-Apr-11 11:06am
What do you mean "it's ending the program"?
Have you tried debugging it? Put a break point on the line "Dim sr As IO...." and single step through the routine when it hits the breakpoint. What happens?
D7000 9-Apr-11 11:18am
i mean it's closing the debugging
OriginalGriff 9-Apr-11 11:39am
When you reply, use the "Reply" widget: that way the appropriate person gets an email. Adding a comment to you question doesn't do that, so replies may be missed or not noticed for a long time.
OriginalGriff 9-Apr-11 11:40am
Seriously? You press the button on your form which is linked to the handler, and Visual Studio closes?
Wendelius 9-Apr-11 11:56am
Just a wild thought. Check that you don't have another event handler attached to button 1. Search for subs having Handles Button1.Click in the end. Perhaps you have another sub which fires first and in that sub you close the application.
Rate this:
Please Sign up or sign in to vote.

Solution 1

Use the debugger. Place a breakpoint to the first line and start going line by line. Use the watches or the quickwatch window to examine the values of the variables etc.

Also using a try-catch[^] structure would be reasonable in your code in order to handle possible IO problems.
Rate this:
Please Sign up or sign in to vote.

Solution 2

Where is the file "AVERAGE.TXT"?

Check it: if it isn't in your projects Bin/Debug directory, then that will be the problem.

Set the full path to the file (which could be "C:\Temp\AVERAGE.TXT" for example) instead, and try that.
D7000 9-Apr-11 11:10am
it is in bin/debug .. so it's not the problem .. but thanks
OriginalGriff 9-Apr-11 11:12am
You're welcome - does it give any error message? Or just "stop"?
D7000 9-Apr-11 11:19am
no, it just stop
OriginalGriff 9-Apr-11 11:23am
Does it get into the routine? If you set a breakpoint on the first line, does it hit it when you press the button?

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

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100