Author |
Message |
CompN
|
Posted: Tue Nov 07, 2006 4:01 pm Post subject: Reading Files Help (Visual Basic) |
|
|
Well I have to find the lowest and highest score given a set of data
This is what I have so far:
Private Sub cmdHighandLow_Click()
Dim name As String
Dim number As String
Dim assists As String
Dim goals As String
Dim gamesWon As String
Dim total As String
Dim count As Integer
Dim highest As Integer
Dim highestName As String
Dim lowest As Integer
Dim lowestName As String
lowest = 0
highest = 0
Cls
Open "f:\Hockey.dat" For Input As #1
total = 0
Do While EOF(1) = False
Input #1, name, number, assists, goals, gamesWon
Label1.Caption = Label1.Caption + name + vbCrLf
Label2.Caption = Label2.Caption + number + vbCrLf
Label3.Caption = Label3.Caption + assists + vbCrLf
Label4.Caption = Label4.Caption + goals + vbCrLf
Label5.Caption = Label5.Caption + gamesWon + vbCrLf
count = count + 1
total = assists + goals * 2
Label6.Caption = Label6.Caption + total + vbCrLf
If total > highest Then
highest = total
highestName = name
Elseif
End If
Loop
Print Tab(150); highest & highestName
Close
End Sub
I know how to find the higest, but I'm having trouble finding the lowest.
|
|
|
|
|
|
Sponsor Sponsor
|
|
|
Silent Avenger
|
Posted: Tue Nov 07, 2006 4:12 pm Post subject: (No subject) |
|
|
If I'm not mistaken this section of code does the checking for the highest: code: | If total > highest Then
highest = total
highestName = name
Elseif |
So you should be able to do the same thing in reverse to get what you would like which is the lowest score.
|
|
|
|
|
|
CompN
|
Posted: Tue Nov 07, 2006 4:18 pm Post subject: (No subject) |
|
|
Silent Avenger wrote: If I'm not mistaken this section of code does the checking for the highest: code: | If total > highest Then
highest = total
highestName = name
Elseif |
So you should be able to do the same thing in reverse to get what you would like which is the lowest score.
are you trying to say:
if total < highest then
..........
I thought that too, but then I have a list of numbers and when I put in that it displays the second highest number.
|
|
|
|
|
|
Silent Avenger
|
Posted: Tue Nov 07, 2006 4:23 pm Post subject: (No subject) |
|
|
Yes just use your lowest variable instead.
|
|
|
|
|
|
CompN
|
Posted: Tue Nov 07, 2006 4:32 pm Post subject: Re: Reading Files Help (Visual Basic) |
|
|
CompN wrote:
Dim highestName As String
Dim lowest As Integer
Dim lowestName As String
lowest = 0
highest = 0
But none of the numbers are below 0.
when you say :
if total < lowest then
..........................
It just appears as zero
I think lowest = ??????????
|
|
|
|
|
|
Silent Avenger
|
Posted: Tue Nov 07, 2006 5:15 pm Post subject: (No subject) |
|
|
Wait are you trying to check for the lowest score for more than one hockey player?
|
|
|
|
|
|
CompN
|
Posted: Tue Nov 07, 2006 5:18 pm Post subject: (No subject) |
|
|
Silent Avenger wrote: Wait are you trying to check for the lowest score for more than one hockey player?
ya
|
|
|
|
|
|
Silent Avenger
|
Posted: Tue Nov 07, 2006 5:37 pm Post subject: (No subject) |
|
|
Well there are a couple issues with your code then. First of all you should use an array when dealing with multiple items you could also use a user define type but you don't really need to for your program. With an array you'll be able to have multiple locations within one type of variable so in your case you would have an array for the naem which would look like this:
Dim name(1 to 20) as String
You would do this for all the stats of the player as well such as games won and goals. In this way you'll have 20 locations in each variable for 20 hockey player info. This will also make it easier to figure out the highest and lowest scores.
|
|
|
|
|
|
Sponsor Sponsor
|
|
|
cool dude
|
Posted: Tue Nov 07, 2006 8:04 pm Post subject: (No subject) |
|
|
there are several issues with your code like silent avenger pointed out. The first thing i want to state is that you should attach your program so that we don't have to redraw the form. Also why are you declaring total as a string and then setting a string equal to zero? Also if you really know how to find the highest you should have no problem finding the lowest. attach your program as an attachment for more help.
|
|
|
|
|
|
RGB255
|
Posted: Tue Nov 07, 2006 8:06 pm Post subject: (No subject) |
|
|
I remember we had to make a program like this and it didn't take nearly as many variables.
|
|
|
|
|
|
Silent Avenger
|
Posted: Tue Nov 07, 2006 9:42 pm Post subject: Re: Reading Files Help (Visual Basic) |
|
|
Wow I can't believe I didn't notice that total was declared as a string good catch on that one cool dude. Also as RGB255 pointed out you do seem to have quite a few variables. You should always try to use the least about of variables possible to make the program more efficient in memory usage. Just by taking a quick look at your program I can tell you right away that you can eliminate highestname and lowestname, as well depending on how you intend to use your program you could also get rid of lowest and highest as well. I have made an example program that you will be able to put in as many different numbers by typing in a number in the text box and clicking the command button on the left side of the screen. This program will also output the highest and lowest numbers in the list and you do this by clicking on the right command button. This is a very simple program and has no error handling code so don't go typing letters in the text box. I've also put the code below just in case you can't open the VB files.
code: | Dim exvar() As Integer
Dim x As Integer
Private Sub Command1_Click()
If x < 20 Then
x = x + 1
ReDim Preserve exvar(x)
exvar(x) = Text1.Text
List1.AddItem exvar(x)
End If
End Sub
Private Sub Command2_Click()
Label2.Caption = exvar(1)
For r = 1 To x
If exvar(r) > Label1.Caption Then Label1.Caption = exvar(r)
If exvar(r) < Label2.Caption Then Label2.Caption = exvar(r)
Next r
End Sub
|
Description: |
|
Download |
Filename: |
Form1.frm |
Filesize: |
2.18 KB |
Downloaded: |
168 Time(s) |
|
|
|
|
|
|
cool dude
|
Posted: Wed Nov 08, 2006 8:08 pm Post subject: (No subject) |
|
|
not sure if you know this silent avenger but you don't have to attach your project you just need your form. this will save you a few mouse clicks.
The following code is how to get the highest number in the file. i purposely didn't include the lowest number code because i want CompN to try and figure it out. a good hint though would be to use an array and set the first index 0 to equal the first number in the file. then compare the next numbers in the file to the first number index and if there are smaller numbers than the first index 0 then set the new smaller number equal to the array.
code: |
Dim nums As Integer
Dim highest As Integer
Open "test.txt" For Input As #1
Do Until EOF(1)
Input #1, nums
If highest < nums Then
highest = nums
End If
Loop
MsgBox "highest " & highest |
|
|
|
|
|
|
Silent Avenger
|
Posted: Wed Nov 08, 2006 8:39 pm Post subject: (No subject) |
|
|
Yes I know all I have to attach is the form sorry for that it's just out of habit. Remember CompN this is only an example and you won't be able to use the code in your program unless you understand how it works. Once you figure out what the code does then you'll be able to use a similar method in your program.
|
|
|
|
|
|
|