VisualBasic6 Project

S

Thread Starter

Sandy Hovers

I need to create a Bowling Score application that allows the user to enter as many bowling scores as they want and then display the high and low scores...

This is what I have, but it won't work...

Private Enter As Integer
Private HighScore As Integer
Private LowScore As Integer

Private Sub cmdEnterScores_Click()

Enter = InputBox("Enter your bowling score:", "Bowling Score")

LowScore = 0
HighScore = 301

Do
If Enter > HighScore Then
HighScore = Enter
End If
Loop While Enter >= HighScore And Enter <= 301


Do
If Enter < LowScore Then
LowScore = Enter
End If
Loop While Enter <= LowScore


End Sub

Private Sub cmdStatistics_Click()
lblDisplay.Caption = "The high score is" & " " & HighScore & " " & "and the low score is" & " " & LowScore
End Sub

Private Sub cmdDone_Click()
Unload Me

HELP ME PLEASE
End Sub
 
B
<p>My guess is that you want something like this ...
<pre>
'Declare constants
Private Const SCORE_HIGH = 300
Private Const SCORE_LOW = 0

Private Sub cmdEnterScores_Click()
Dim strEnter As String
Dim iValue As Integer
Dim iLowScore As Integer
Dim iHighScore As Integer


'Initialize values
iHighScore = 0
iLowScore = 300

Do While True

'Display input box
strEnter = InputBox("Enter your bowling score:", "Bowling Score")

If Len(strEnter) < 1 Then Exit Do 'Exit loop is the return string is "" or cancel

If IsNumeric(strEnter) Then 'Check to see that the return value is numeric
If iValue > SCORE_HIGH Or iValue < SCORE_LOW Then Exit Do

iValue = Val(strEnter) 'Convert the string retrieved from the input box to a value

If iValue > iHighScore Then
iHighScore = iValue
End If


If iValue < iLowScore Then
iLowScore = iValue
End If

Else
'Prompt user to enter a valid score
MsgBox "Please enter a valid score", vbExclamation, "Score Manager"

End If
Loop

lbldisplay.Caption = "The high score is" & " " & iHighScore & " " & "and the low score is" & " " & iLowScore


End Sub


Private Sub cmdDone_Click()
Unload Me
End Sub
</pre>
 
J
Without entering any data, and just a cursory look at your code, I think the problem is in the initial values of LowScore and HighScore. Try
reversing the initialization to:

HighScore = 0
LowScore = 301

Also, initialize them outside of the Sub, or they will be reset everytime you enter another value. You seem to be declaring them globally anyway,
so initialize them in the original declaration.

The reason is that since a bowling score is always between 0 ond 300, Enter will never be greater than HighScore as you have it defined, so your If statement can never be true. By setting HighScore to 0 initially, the first vale of Enter will be assigned to HighScore, since it will
inherently be greater than 0 (unless you had a _REALLY_ bad game)

Likewise with LowScore. If you set it to 0, then Enter can never be lower, and your If statement is always false. By setting it to 301, the
first value of Enter will also be set to LowScore, and the comparisons will work.

For example, Lets say we have 4 games, with scores of 225, 150, 280, and 200

We set LowScore to 301, and HighScore to 0 as part of the initialization -outside- of cmdEnterScores_Click()

So, initially (before any Sub execution) you have:

Enter = Undefined
HighScore = 0
LowScore = 301

After the first pass thru the Sub, (where we enter the first game, ie: 225) we have:

Enter = 225
HighScore = 225
LowScore = 225

This is because Enter was greater than HighScore (0) and Less Than LowScore( 301)

All is good. Click the button again, and enter the 2nd game, 150. Now we have

Enter = 150
HighScore = 225
LowScore = 150

Because Enter was not greater than HighScore (225) that value did not change, but it WAS less than LowScore (225) so LowScore becomes 150

Third time thru, entering 280, we end up with

Enter = 280
HighScore = 280
LowScore = 150

Enter was greater than Highscore, so that updated, but not less than LowScore, so it stayed the same.

Finally, the last game which scored 200. We end up with

Enter = 200
HighScore = 280
LowScore = 150

Since Enter was not Greater than Highscore, and also was not less than LowScore, neither of those values changed.

Do you also see why you cannot reset the values of HighScore and LowScore in the subroutine? If you did, then it would always behave like the first time through, ie: Enter, HighScore and LowScore would always be the same value, which would be whatever value you entered last. By defining them to the opposite extreme of their possible range, you insure that they will
hold real, application specific data after the first pass. By defining them outside of the main program loop, you make sure that they retain ther
application specific data through the life of your program.

Hope that this rather long winded explanation helps!

--Joe Jansen
 
could you send me the interface, please?
dont worry, I dont want to steal it.
Maybe I can help you.:)
 
D

David Wooden

It looks like your code is trying to loop through an array of scores, but you only have three scores: HighScore, LowScore and Enter. If you just compare and replace once for each entry, you've got it covered. the do loops serve no purpose. Another problem is that if HighScore is initialized to 301, no score will ever be higher. Likewise, no score will ever be lower than 0.

A really quick and slick way to do this would be to add the new scores into a sorted listbox. You wouldn't have to calculate anything; the listbox will handle the sorting for you.

Best regards,

David Wooden
Senior Software Engineer, Systems Integration
TAS Division of Omron Electronics LLC
Office: (847) 884-7034 Extension 432
Fax: (847) 884-9383
E-mail: [email protected]
 
Top