|
I did something wrong...
Link |
by
|
|
ok... i'm in a programming course at my school and i have to do these reviews from a textbook. We use visual basic and i have to make an appplication that will tell the user the number of times a letter occurs in a word. With what i have, it says there is an overflow error and i dont know how to fix it. The information it gives me is: intnumoccurances = 0 intsearchpos = 0 do intletterpos = position of letter within phrase if intletterpos > 0 then increment intnumoccurances intsearchpos = intletterpos + 1 end if loop while intsearchpos <= len(strphrase) and intletter pos <> 0 ____________________________________________________________________________ And this is what i have (please don't diss the programming style too much) _____________________________________________________________________________ Option Explicit
Private intnumoccurances As Integer, intsearchpos As Integer
Private strtext As String, strsubstring As String, intletterpos As Integer
Private Sub Form_Load()
intnumoccurances = 0
intsearchpos = 1
End Sub
Private Sub cmdcount_click()
strtext = txttext.Text
strsubstring = txtletter.Text
intletterpos = InStr(intsearchpos, strtext, strsubstring)
Do While intsearchpos <= Len(strtext) And intletterpos <> 0 And InStr(intsearchpos, strtext, strsubstring) > 0
If intletterpos > 0 Then
intnumoccurances = intnumoccurances + 1
intsearchpos = intletterpos + 1
Else
intnumoccurances = 0
lbloccurances.Caption = intnumoccurances
End If
Loop
lbloccurances.Caption = intnumoccurances
End Sub
Private Sub cmddone_click()
Unload Me
End Subcan anyone please tell me what im doing wrong...?
"There are few people i would sacrifice something for and many people i would sacrifice a lot for, but there are only two people i would sacrifice everything for." ~ A quote by me! ~ (I think its original)
|
|
Re: I did something wrong...
Link |
by
|
What you had would put you in an infinite loop where a variable would eventually reach its limit. I don't know about you, but this makes a lot more sense to me:Option Explicit
Private Sub cmdcount_click()
If Not txtletter.TextLength = 1 Then
MsgBox("We one (and only need one) letter to search for. Thanks for trying")
Return
End If
Dim strtext As String
Dim strsubstring As Char
Dim intletterpos, intsearchpos, intnumoccurances As Integer
intsearchpos = 1
intnumoccurances = 0
strtext = txttext.Text
strsubstring = txtletter.Text
intletterpos = InStr(intsearchpos, strtext, strsubstring)
For Each c As Char In strtext
If c = strsubstring Then
intnumoccurances += 1
End If
Next
lbloccurances.Text = intnumoccurances
End Sub
Private Sub cmddone_click()
Close()
End Sub |