Computer Science Canada Reading Files Help (Visual Basic) |
Author: | CompN [ 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. |
Author: | Silent Avenger [ Tue Nov 07, 2006 4:12 pm ] | ||
Post subject: | |||
If I'm not mistaken this section of code does the checking for the highest:
So you should be able to do the same thing in reverse to get what you would like which is the lowest score. |
Author: | CompN [ Tue Nov 07, 2006 4:18 pm ] | ||
Post subject: | |||
Silent Avenger wrote: If I'm not mistaken this section of code does the checking for the highest:
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. |
Author: | Silent Avenger [ Tue Nov 07, 2006 4:23 pm ] |
Post subject: | |
Yes just use your lowest variable instead. |
Author: | CompN [ 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 = ?????????? |
Author: | Silent Avenger [ Tue Nov 07, 2006 5:15 pm ] |
Post subject: | |
Wait are you trying to check for the lowest score for more than one hockey player? |
Author: | CompN [ Tue Nov 07, 2006 5:18 pm ] |
Post subject: | |
Silent Avenger wrote: Wait are you trying to check for the lowest score for more than one hockey player?
ya |
Author: | Silent Avenger [ Tue Nov 07, 2006 5:37 pm ] |
Post 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. |
Author: | cool dude [ Tue Nov 07, 2006 8:04 pm ] |
Post 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. |
Author: | RGB255 [ Tue Nov 07, 2006 8:06 pm ] |
Post subject: | |
I remember we had to make a program like this and it didn't take nearly as many variables. |
Author: | Silent Avenger [ 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. ![]()
|
Author: | cool dude [ Wed Nov 08, 2006 8:08 pm ] | ||
Post 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.
|
Author: | Silent Avenger [ Wed Nov 08, 2006 8:39 pm ] |
Post 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. |