19.12.2012 Views

Computer Programming Concepts and Visual Basic David I. Schneider

Computer Programming Concepts and Visual Basic David I. Schneider

Computer Programming Concepts and Visual Basic David I. Schneider

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

168 <strong>Computer</strong> <strong>Programming</strong> <strong>Concepts</strong> <strong>and</strong> <strong>Visual</strong> <strong>Basic</strong><br />

EXAMPLE 5<br />

Suppose the data file WINNERS.TXT contains the names of the teams who have won each of the World<br />

Series, with the first line of the file giving the number of World Series that have been played. Write a program<br />

that displays the numbers, if any, of the World Series that were won by a team specified by the user.<br />

’Create form-level array<br />

Dim teamName() As String<br />

Dim seriesCount As Integer<br />

Private Sub cmdDidTheyWin_Click()<br />

Dim teamToFind As String, numWon As Integer, series As Integer<br />

’Search for World Series won by user’s team<br />

teamToFind = UCase(txtName.Text)<br />

numWon = 0<br />

picSeriesWon.Cls<br />

For series = 1 To seriesCount<br />

If UCase(teamName(series)) = teamToFind Then<br />

numWon = numWon + 1<br />

If numWon = 1 Then<br />

picSeriesWon.Print “The ”; teamName(series);<br />

picSeriesWon.Print “ won the following World Series: ”;<br />

Else<br />

‘Separate from previous<br />

picSeriesWon.Print “,”;<br />

If (numWon = 5) Or (numWon = 16) Then<br />

‘Start a new line at 5th <strong>and</strong> 16th win<br />

picSeriesWon.Print<br />

End If<br />

End If<br />

‘First world series played in 1903<br />

picSeriesWon.Print Str(series + 1902);<br />

End If<br />

Next series<br />

If numWon = 0 Then<br />

picSeriesWon.Print “The ”; teamToFind; “ did not win any World Series.”<br />

End If<br />

End Sub<br />

Private Sub Form_Load()<br />

Dim series As Integer<br />

‘Fill array with World Series winners<br />

Open “WINNERS.TXT” For Input As #1<br />

Input #1, seriesCount<br />

ReDim teamName(1 To seriesCount) As String<br />

For series = 1 To seriesCount<br />

Input #1, teamName(series)<br />

Next series<br />

Close #1<br />

End Sub<br />

[Run, type Yankees into the text box, <strong>and</strong> click the comm<strong>and</strong> button.]

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!