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()
bob sagetAsked:
Who is Participating?
 
Fernando SotoConnect With a Mentor RetiredCommented:
Hi Bob;

The following code snippet is how I would have written it.
'' Will hold the value if numeric from the InputBox
'' No need for Double here Integer works fine
Dim questfile As Integer
'' Holds the input from the user
Dim inputData As String

'' Using the Using statement as below will close the file automatically when it leaves the Using statement.
Using myfile As IO.StreamWriter = IO.File.CreateText("question2.txt")
    '' Read user input
    inputData = InputBox("enter one words to be outputed in a text file: enter -1 to stop")
    '' Parse the input to see if it is a numeric value and place the numeric data in questfile
    Integer.TryParse(inputData, questfile)
    '' Test to see if we are done
    Do While questfile <> -1
        '' Write data to the file system
        myfile.WriteLine(inputData)
        '' Read user input
        inputData = InputBox("enter one words to be outputed in a text file: enter -1 to stop")
        '' Parse the input to see if it is a numeric value and place the numeric data in questfile
        Integer.TryParse(inputData, questfile)
    Loop
End Using

Open in new window

Also in your last post you had this line of code
MessageBox.Show("File Does Not Exist") 

Open in new window

This will never execute because IO.File.CreateText will create it if it does not exisit.
0
 
Fernando SotoRetiredCommented:
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

0
 
bob sagetAuthor Commented:
how would i do it so if i entered a letter it would transfer it to the text file?
0
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
Fernando SotoRetiredCommented:
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?
0
 
bob sagetAuthor Commented:
words. But does it matter if i enter a single letter or a word?
0
 
Fernando SotoRetiredCommented:
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?
0
 
bob sagetAuthor Commented:
word per line
0
 
bob sagetAuthor Commented:
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")
0
 
bob sagetAuthor Commented:
thx for your help
0
 
Fernando SotoRetiredCommented:
Not a problem Bob, glad to help.
1
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.