CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    Dec 2002
    Location
    Birmingham UK
    Posts
    83

    Angry My Logic is well mess need help with System.IO

    Hi guys

    I really would appericate if you can either point me to the right direction or give me a example of how it could be done.

    As part of my University project I am writting a program that basically helps people with spelling the objective are

    App will speak out the word and user will type the word if match found then you get a point if not then you get a point for getting it wrong so at the end of spelling list you will know how many you got right and how many you got wrong.

    the problem is my code a does not generate random word from my list of spelling and B it just runs from begaining of the file to the end of the file with out giving the user the option to enter a new word after the app has spoken .

    here is my code
    Code:
     ' Declare a variable for system.IO as streamReader 
            Dim ObjReader As New System.IO.StreamReader("C:\Users\waheedrafiq\Dropbox\University BCU\Visual Basic ICT\Basic_AI\EnglishSpellingABC\englishspelling.txt")
            Dim Count As Integer
            Dim CountWrongAnswer As Integer
    
            Dim sLine As String = ""  ' Declare sLine as string variable to be use in the loop
            Dim userInput As String    ' declare UserInput as string to be use to store user value.
    
    
            Count = 0
            CountWrongAnswer = 0
    
    
    
            Do While ObjReader.EndOfStream = False ' Do while we have not reach end of file.
    
    
                sLine = ObjReader.ReadLine()   ' Read from file and store value to sLine
                speakWord.Speak(sLine)          ' Speak the value store in sLine
                sLine.ToUpper().Trim() '  convert sLine value to Uppercase remove any spaces  
    
    
                '   userInput = InputBox("Enter correct spelling" & vbOKOnly)
    
    
                userInput = txtUserInput.Text ' Store value from textbox to userinput variable
    
                userInput.ToUpper().Trim()  ' covert userInput value to uppercase and remove any spaces
    
    
                If sLine.ToUpper.Trim = userInput.ToUpper.Trim Then
    
                    speakWord.Speak("Well done that was the correct answer")
    
    
    
                    Count += 1
    
    
                    lblScore.Text = CStr(Val(Count.ToString))
    
    
    
    
                ElseIf ObjReader.EndOfStream = True Then
    
                    speakWord.Speak("Spelling test has ended well done " & "You have Scored" & Count & "Out of " & CountWrongAnswer)
    
                    ObjReader.Close()
    
                    ' Exit Do
    
    
    
    
                Else
    
                    speakWord.Speak("Wrong spelling")
    
                    CountWrongAnswer += 1
                    lblWrongScore.Text = CStr(Val(CountWrongAnswer.ToString))
    
    
                End If
            Loop
    
        End Sub

    any support will be much appreciated
    Last edited by GremlinSA; November 20th, 2013 at 12:48 AM. Reason: added code tags...
    Waheed Rafiq

    www.waheedrafiq.com


    MCP x 2 , Network+.CNNA

  2. #2
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: My Logic is well mess need help with System.IO

    You'd need a routine to sort the file as soon as you read it. Read it into an array or listbox, and then use a few RANDOMIZE functions to pick a new word to speak.
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  3. #3
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,362

    Re: My Logic is well mess need help with System.IO

    You can use File.ReadAllLines() to easily get the data into an array then as mentioned above use a random call to pull out one of those from the resulting array as needed
    Always use [code][/code] tags when posting code.

  4. #4
    Join Date
    Dec 2002
    Location
    Birmingham UK
    Posts
    83

    Re: My Logic is well mess need help with System.IO

    Thanks guys , I shall take on above advice and see what I can come up with . will post a new result here

    cheers for the help
    Waheed Rafiq

    www.waheedrafiq.com


    MCP x 2 , Network+.CNNA

  5. #5
    Join Date
    Dec 2002
    Location
    Birmingham UK
    Posts
    83

    Re: My Logic is well mess need help with System.IO

    Hi guys

    I have given my best shot at this , I haven't touch vb for 13 years , currently I am studying the topic again at my university hence the interest.

    so much has change in what was Visual Basic is not Visual dot net ,

    here is my code :

    Code:
    ' Declare a variable for system.IO as streamReader 
    
            Dim ObjReader As New System.IO.StreamReader("C:\Users\waheedrafiq\Dropbox\University BCU\Visual Basic ICT\Basic_AI\EnglishSpellingABC\englishspelling.txt")
            Dim path As String = "C:\Users\waheedrafiq\Dropbox\University BCU\Visual Basic ICT\Basic_AI\EnglishSpellingABC\englishspelling.txt"
    
    
            Dim Count, ArrayCount As Integer
            Dim CountWrongAnswer As Integer
            Dim ArrayOfTextLine() As String
    
    
            Dim sLine As String = ""  ' Declare sLine as string variable to be use in the loop
            Dim userInput As String    ' declare UserInput as string to be use to store user value.
    
            ArrayCount = 0
    
            Count = 0
            CountWrongAnswer = 0
    
    
    
    
    
            Do While ObjReader.EndOfStream = False ' Do while we have not reach end of file.
    
    
    
                txtThinking.Text = "ObjReader should have read the file" & " you are on Array[" & ArrayCount & "]" & vbCrLf
    
    
    
                sLine = ArrayOfTextLine(CInt(Rnd(ArrayCount)))
    
    
    
                ' sLine = Array(ArrayCount)
                'sLine = ObjReader.ReadLine()   ' Read from file and store value to sLine
                speakWord.Speak(sLine)          ' Speak the value store in sLine
                sLine.ToUpper().Trim() '  convert sLine value to Uppercase remove any spaces  
    
                ArrayCount = ArrayCount + 1
    
    
    
                '   userInput = InputBox("Enter correct spelling" & vbOKOnly)
    
    
                userInput = txtUserInput.Text ' Store value from textbox to userinput variable
    
                userInput.ToUpper().Trim()  ' covert userInput value to uppercase and remove any spaces
    
    
                If sLine.ToUpper.Trim = userInput.ToUpper.Trim Then
    
                    speakWord.Speak("Well done that was the correct answer")
    
    
    
                    Count += 1
    
    
                    lblScore.Text = CStr(Val(Count.ToString))
    
    
    
    
                ElseIf ObjReader.EndOfStream = True Then
    
                    speakWord.Speak("Spelling test has ended well done " & "You have Scored" & Count & "Out of " & CountWrongAnswer)
    
                    ObjReader.Close()
    
                    Exit Do
    
    
    
    
                Else
    
                    speakWord.Speak("Wrong spelling")
    
                    CountWrongAnswer += 1
                    lblWrongScore.Text = CStr(Val(CountWrongAnswer.ToString))
    
    
                End If
    
            Loop
    
    
    
        End Sub
    at one point I could get the program to read into array but I can't get it to randomize it even if I was to use this method

    txtbox.text = arraryofTextLine(Cint(rnd(arraycount))) , it won't randomize and hence can't display the context of the file.


    what am I doing wrong in my logics


    help
    please
    Last edited by DataMiser; November 23rd, 2013 at 09:50 AM. Reason: fixed code tags
    Waheed Rafiq

    www.waheedrafiq.com


    MCP x 2 , Network+.CNNA

  6. #6
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,362

    Re: My Logic is well mess need help with System.IO

    You're not using random correctly

    At the top of the code just under the class header but before any routines add

    Private Rnd1 as New Random

    In your code use Rnd1.Next(MaxNumber)

    see examples of how to use Random in VB.Net
    http://msdn.microsoft.com/en-us/libr...vs.110%29.aspx
    Always use [code][/code] tags when posting code.

  7. #7
    Join Date
    Dec 2002
    Location
    Birmingham UK
    Posts
    83

    Re: My Logic is well mess need help with System.IO

    Hi guys

    I have kind of mess my logic up here , all I want to do is read a word randomly from one file ,I haven't got a clue of where to start.


    I have experimented with my code above and just can't get it to work , I have tried 7 different ways of reading a file and placing it into array and I still can't get a random word

    this is what I am thinking


    Code:
     
    
    
    
     Dim ObjReader As New System.IO.StreamReader(Application.StartupPath & "\Spelling\englishspelling.txt")
     Dim ArrayWords()  as string 
    
            Count = 0   ' intialize count to 0
            CountWrongAnswer = 0 ' intialize  CountWrongAnswer to 0 
    
            Do While ObjReader.EndOfStream = False
    
               'This is where I am stuck
                ArrayWords(1) = ObjReader.ReadLine()  ' I get a error  and then the Million dollar question just how do I Read from a file a random word.
    
    
                sLine = ObjReader.ReadLine()   ' Store the value in text file to sLine
                speakWord.Speak(sLine)    ' Speak to the value store in Sline
                userInput = InputBox("Type your word")   ' Ask your to type what was spoken by the system.
                If sLine.ToUpper.Trim = userInput.ToUpper.Trim Then  ' value in sLine convert To uppercase and trim any spaces equal to userInput convert to uppercase and trim any spaces
                    speakWord.Speak("Well done that was the correct answer")
                    Count += 1
                    lblScore.Text = CStr(Val(Count.ToString))
    
                ElseIf ObjReader.EndOfStream = True Then
    
                    speakWord.Speak("Spelling test has ended well done " & "You have Scored" & Count & "Out of " & CountWrongAnswer)
                    ObjReader.Close()
                    Exit Do
    
                Else
                    speakWord.Speak("Wrong spelling")
                    CountWrongAnswer += 1
                    lblWrongScore.Text = CStr(Val(CountWrongAnswer.ToString))
    
                End If
            Loop
    
        End Sub

    Any code example would be very , very much appreciated
    Waheed Rafiq

    www.waheedrafiq.com


    MCP x 2 , Network+.CNNA

  8. #8
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,362

    Re: My Logic is well mess need help with System.IO

    You should read the file into an array using readalllines method. Note this should be done in a different routine and only needs to be done once
    You need to make a call to create a new instance of random as well

    Then in your sub where you want to get a random word you simply use .Next method of random to get the index of the word from the array

    The code you have shown does not appear to even attempt to get a random value
    Always use [code][/code] tags when posting code.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured