I am a noob to VB and have tried messing with this code for hours now.
Code:
Private Sub btnPres_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPres.Click
Dim strFileName As String = CurDir() & "\USPres.TXT"
Dim sr As IO.StreamReader = New IO.StreamReader(strFileName)
Dim strPres As String
Dim intCounter, intNum As Integer
intNum = CInt(txtInput.Text)
intCounter = 1
If intNum > 44 Then
MessageBox.Show("you need to type an integer between 1 and 44 inclusice.", "U.S. Presidents", MessageBoxButtons.OK)
End If
Do While sr.Peek <> -1
strPres = CStr(sr.ReadLine)
Loop
sr.Close()
If sr.Peek = 0 Then
strPres = CStr(sr.ReadLine)
If intNum = intCounter Then
intCounter = intCounter + 1
Else : txtOutput.Text = strPres & " Was President number " & txtInput.Text
End If
End If
End Sub
How do I get the line of the file to correspond with the number that is inputted in the text box? The file looks like this...
George Washington
John Adams
Thomas Jefferson
James Madison
James Monroe
John Quincy Adams
Andrew Jackson
Martin Van Buren
William Henry Harrison
ect....
You are lacking a lot of structure there - do you understand how the program execution will flow through the routine.
For a start if the user enters a number outside of 1-44 they will get a message but it will then proceed and try and match it, but I would have thought you'd want to bail out from there.
Then you loop through and read the data from the text box via a streamreader until Peek returns -1
Then you close your streamreader.
Then you call Peek again on the streamreader that you've just closed.
You are also incrementing your counter only if it = the number you're looking for when surely it should be the other way around.
Have you put a breakpoint on the first line and stepped through your routine - that will help you understand where it is going.