r/visualbasic • • Feb 01 '16

[VB2015] User Input Loop question

Hello I am working on an visual basic assignment. For what its worth I got it to work/filled the requirements for it but I want to try to get it to work in a loop. So I did a do while loop. And that works as well...but can't exit out of the loop leading me into a infinite loop. Is there anyway for me to exit out properly?

Code pasted here: http://paste.ofcode.org/6Qn5ViBt33442Fwum2CRyZ

6 Upvotes

3 comments sorted by

3

u/PostalElf VB.Net Intermediate Feb 01 '16

"Exit While" allows you to manually quit a loop.

2

u/ViperSRT3g Application Specialist Feb 01 '16

You forgot to set the value of numInt to something other than -1. Your whole loop there is correctly formatted (aside from not declaring the variable locally) but you don't have anything in there that would affect the conditions of your loop. What the logic is stating in your code, is you type in -1 to start the process. Once it has started, the variable numInt remains at -1 because nothing else changes its value. So you can enter in all the inputs you want, and it will keep asking you for more inputs. If you set the value of numInt to anything else besides -1 when you input a letter, your loop would end immediately.

2

u/Air_chandler Feb 01 '16 edited Feb 01 '16

Surely here you want to declare the input inside the Do loop then have if statements say if input = "a" do sound then elseif input = "something else" do something else, and have elseif input = "exit" to exit the loop as you wanted (use exit do in this case), because looking at this your logic is unclear to me.

For example:

Do

dim input as Char

Console.writeline("Enter a letter to hear a sound, enter E to exit")

input = console.readline()

If input = "x" do sound

elseif input = "other x" do another sound

elseif input = "E" exit do

else console.writeline("wrong input")

loop

In this example the user will be asked to input a letter to play corresponding sound which then input takes this value, and will keep being asked until they enter E to exit the loop.