AllyOmega
AllyOmega

Reputation: 45

vb.net console application only works once

I'm just learning to code in vb.net and am currently messing around with VB.net console applications. I can't for the life of me figure something out. It's probably been asked before on here, but I can't find anything by searching. I just coded a simple "check if Y or N was entered. If y/n was entered, display 'you have entered y/n'" program, and it works fine the first time. However, after the first entry I can't get the process to repeat. All I get back is blank space. For example, if i enter y, I'll get the corresponding message. however, if after that I enter n I get nothing back.

here's the code:

Module Module1

Sub Main()

    Console.Title = "Hello"
    Console.WriteLine("Y or N")
    Dim line As String
    line = Console.ReadLine()
    Do Until line = "exit"
        If line = "y" Then
            Console.WriteLine("you have chosen y")
            Console.ReadLine()
        ElseIf line = "n" Then
            Console.WriteLine("you have chosen n")
            Console.ReadLine()
        End If
        line = ""
    Loop
End Sub

End Module

I'm assuming the answer's super simple, but I can't seem to figure it out or fin the answer online.

Thanks for the help.

Upvotes: 2

Views: 3914

Answers (5)

Dan Puzey
Dan Puzey

Reputation: 34218

Inside your loop, you're doing Console.ReadLine() without doing anything with the value, and you're then doing line = "". Your loop is going around infinitely with an empty line, and ignoring the user input.

Remove the two lines Console.ReadLine(), and then replace line = "" with line = Console.ReadLine().

Upvotes: 0

Jonathan
Jonathan

Reputation: 12015

You have to store the value of Console.ReadLine() in the Line string.

Module Module1

Sub Main()

    Console.Title = "Hello"
    Console.WriteLine("Y or N")
    Dim line As String
    line = Console.ReadLine()
    Do Until line = "exit"
        If line = "y" Then
            Console.WriteLine("you have chosen y")
        ElseIf line = "n" Then
            Console.WriteLine("you have chosen n")
        End If
        line = Console.ReadLine()
    Loop
End Sub

End Module

Upvotes: 1

Pondidum
Pondidum

Reputation: 11637

You need to re-assign line to whatever the next line read from the console is, like so:

Module Module1

    Sub Main()

        Console.Title = "Hello"
        Console.WriteLine("Y or N")
        Dim line As String
        line = Console.ReadLine()
        Do Until line = "exit"

            If line = "y" Then
                Console.WriteLine("you have chosen y")
            ElseIf line = "n" Then
                Console.WriteLine("you have chosen n")
            End If

            line = Console.ReadLine()   ''here

        Loop
    End Sub

End Module

Upvotes: 0

andrux
andrux

Reputation: 2922

This line is your problem:

line = ""

You're reading the console but not assigning that to your variable.

Here's how it should be:

Do Until line = "exit"
    If line = "y" Then
        Console.WriteLine("you have chosen y")
    ElseIf line = "n" Then
        Console.WriteLine("you have chosen n")
    End If
    line = Console.ReadLine()
Loop

Upvotes: 0

user27414
user27414

Reputation:

you need to assign Console.Readline() to line in your Do Loop:

Do Until line = "exit"
    If line = "y" Then
        Console.WriteLine("you have chosen y")
    ElseIf line = "n" Then
        Console.WriteLine("you have chosen n")            
    End If
    line = Console.ReadLine()
Loop

Upvotes: 1

Related Questions