Link to home
Start Free TrialLog in
Avatar of bob saget
bob saget

asked on

do while loop for putting information in a text file

i have a text file inside my bin-debug file ( its empty). my code is supposed to be a do while loop, and when the user presses a button they are supposed to enter 3 words in, once they do they that, those words will be inputted inside my code, this is my code right here. when it runs, it won't output the letters, and it will stay in an infinite loop.




Dim myfile As IO.StreamWriter = IO.File.CreateText("question2.txt")
        Dim questfile As Double

        questfile = CDbl(InputBox("enter 3 words to be outputed in a text file: enter -1 to stop"))

        Do While CDbl(questfile) <> -1
            myfile.WriteLine(questfile)
            questfile = CDbl(InputBox("enter 3 words to be outputed in a text file: enter -1 to stop"))
        Loop
        myfile.Close()
Avatar of Fernando Soto
Fernando Soto
Flag of United States of America image

Hi Bob;

Please see my comments in your code
'' You open a file for writing
Dim myfile As IO.StreamWriter = IO.File.CreateText("question2.txt")
'' Your test variable to see if you should exit loop
Dim questfile As Double

'' This line will throw an exception when the input to the InputBox are NOT numbers.
'' In other words trying to convert words to numbers can not be done.
questfile = CDbl(InputBox("enter 3 words to be outputed in a text file: enter -1 to stop"))

'' If the above were corrected then this following line does not need to be converted from a Double
'' to a Double so no need for CDble function
Do While CDbl(questfile) <> -1
    myfile.WriteLine(questfile)
    '' Same error as above
    questfile = CDbl(InputBox("enter 3 words to be outputed in a text file: enter -1 to stop"))
Loop
myfile.Close()

Open in new window

Avatar of bob saget
bob saget

ASKER

how would i do it so if i entered a letter it would transfer it to the text file?
To your question, "how would i do it so if i entered a letter it would transfer it to the text file? ", Letters or words? How do you want it to show up in the file?
words. But does it matter if i enter a single letter or a word?
To the question, "words. But does it matter if i enter a single letter or a word? " it only matters how you want it to be written to the text file, for example one character per line or word per line or maybe everything on one line?
word per line
this code works, but how can i put this is a loop?


Dim MYfile As String = "question2.txt"
        If System.IO.File.Exists(MYfile) = True Then
            Dim writer As New System.IO.StreamWriter(MYfile)
            writer.Write(InputBox("enter 3 words to be outputed in a text file: enter -1 to stop"))
            writer.Close()
            MessageBox.Show("Text written to file")

        Else

            MessageBox.Show("File Does Not Exist")
ASKER CERTIFIED SOLUTION
Avatar of Fernando Soto
Fernando Soto
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
thx for your help
Not a problem Bob, glad to help.