Results 1 to 9 of 9

Thread: Need help with a loop in vb.net console

  1. #1

    Thread Starter
    New Member
    Join Date
    Aug 2013
    Posts
    2

    Need help with a loop in vb.net console

    I am really new to VB and have a question about loops. I am programming a console based text adventure and need to know how to make the wrong choice loop back to the selection. By wrong, I mean something that's not even a choice. Like below, when someone puts in '3'. I want it to say ("What is so hard about '1' or '2'. Pick a number between 1 and 2....") and then repeat the question with the choices. Any help is appreciated.

    Console.WriteLine("Do you want to quit or get started? Type the corresponding number")
    Console.WriteLine("and press ENTER to submit your choice.")
    Console.WriteLine()
    Console.WriteLine("1. Get Started!")
    Console.WriteLine("2. No Thanks!")

    Begin = Console.ReadLine()
    If Begin = 1 Then
    Console.WriteLine("Let's get started!")
    Console.ReadKey()
    ElseIf Begin = 2 Then
    Console.WriteLine("See ya next time!")
    Console.ReadKey()
    Else
    Console.WriteLine("What is so hard about '1' or '2'. Pick a number between 1 and 2....")
    Console.ReadLine()
    End If


    PS. Sorry for no indentions. kinda new to the forum thing, too.

  2. #2
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,715

    Re: Need help with a loop in vb.net console

    Hi Kadoza, welcome to the forum! Please, whenever you post code, post them in code tags:

    [code]
    'Code here
    [/code]

    Try something like this:

    Code:
    Sub Main()
       Call Start
    End Sub
    
    Private Sub Start()
        Console.WriteLine("Do you want to start your adventure? 1 - Yes or 2 - No")
      
        Dim Begin As String
        Begin = Console.ReadLine()
        If Begin = "1" Then
            Console.WriteLine("Let's get started!")
            'Call your next sub here
        ElseIf Begin = "2" Then
            Console.WriteLine("See ya next time!")
            Console.ReadKey()
        Else
            Console.WriteLine("Invalid entry.")
            Call Start()
        End If
       
    End Sub
    Edit - I actually made a console app slots game a while back and recently updated it. The reason I mention this is because it's exactly what you need even though it's a different type of game(mine's casino where as yours is rpg). Check it out here.
    Last edited by dday9; Aug 12th, 2013 at 03:52 PM.
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  3. #3
    Frenzied Member
    Join Date
    Feb 2008
    Location
    Texas
    Posts
    1,288

    Re: Need help with a loop in vb.net console

    You can look into the Do/While conditional loop. That should give you what you're wanting.
    You down with OOP? Yeah you know me!
    MCAD and MCMICKEYMOUSE (vb.net)

    ----

    If it even kinda helps... rate it : )

    Edit a Multi-page .tif file and save.

  4. #4
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    38,989

    Re: Need help with a loop in vb.net console

    First off, I totally support the sarcastic response you are intending for an out of range value.

    Since you are starting out, do yourself a favor and turn Option Strict ON for the project. That can be found on the Compile tab of Propject|Properties. What this will do will be to force you to use proper type conversions rather than relying on the slow (and occasionally wrong) implicit type conversions. For example, ReadLine will return a string, but Begin is clearly an integer. The string should be properly converted to an integer using Integer.TryParse.

    Next, I think that do loop would probably work fairly well here. It would look something like this:

    Code:
    Do
      If integer.TryParse(console.ReadLine,Begin) Then
       'It was an integer, and is now in Begin. 
      Else
       'It wasn't an integer, so Begin will hold 0.
      End If
      If Begin < 1 OrElse Begin > 2 Then
       Begin = 0
        'Scold the user
      End If
    Loop While Begin = 0
    That code is a bit overwrought, since there really isn't a use for the Else and not really a use for the If, either, but I thought an example of what you can do with Integer.TryParse might be in order.
    My usual boring signature: Nothing

  5. #5

    Thread Starter
    New Member
    Join Date
    Aug 2013
    Posts
    2

    Re: Need help with a loop in vb.net console

    Thanks. Got the loop working now. Just a one more question.

    What are the commands for clearing the console and exiting the console. And any other commands that may be useful.

  6. #6
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,715

    Re: Need help with a loop in vb.net console

    Tale a look at the MSDN documentation here.
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  7. #7
    Fanatic Member Flashbond's Avatar
    Join Date
    Jan 2013
    Location
    Istanbul
    Posts
    646

    Re: Need help with a loop in vb.net console

    If I were you, I would use a GoTo.

  8. #8
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,715

    Re: Need help with a loop in vb.net console

    Quote Originally Posted by Flashbond View Post
    If I were you, I would use a GoTo.
    :O

    Please tell me that was a sick joke meant for the VB6 forum?
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  9. #9
    Fanatic Member Flashbond's Avatar
    Join Date
    Jan 2013
    Location
    Istanbul
    Posts
    646

    Re: Need help with a loop in vb.net console

    In fact no, I still do use it if I can't manage so many nested loops

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