CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3

Hybrid View

  1. #1
    Join Date
    Feb 2015
    Posts
    1

    Instead of "goto"-method?

    Hi!
    I'm fairly new to programming, and I've realized that people reaaalllyy don't like "goto"-methods. I was wondering how one might write code that allows a user to decide how many entries to put in? For example, in the code below, the user inputs a name, and is then asked wether he/she wants to input another name. How could I do this without having to use go-to method?

    Thanks in advance

    Code:
    using System;
    
    namespace GoToTest
    {
        public class GoToTest
        {
            public static void Main()
            {
                InputName:
                string name;
                Console.Write("Input name: ");
                name = Console.ReadLine();
    
                string decision;
                Console.WriteLine(""); //Empty line for increased readability
                Console.WriteLine("Would you like to input another name? Yes - No");
                decision = Console.ReadLine();
                if (decision == "Yes")
                {
                    goto InputName;
                }
                else
                {
                    Console.WriteLine("Name is " + name);
                }
    
    
    
            }
        }
    }

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,398

  3. #3
    Join Date
    Apr 2014
    Location
    in northeast ohio
    Posts
    94

    Re: Instead of "goto"-method?

    Code:
            using System;
    
            namespace GoToTest
            {
                public class GoToTest
                {
                    public static void Main()
                    {
                        string name;
                        Console.Write("Input name: ");
                        name = Console.ReadLine();
    
                        string decision;
                        bool isUserExiting = false;
                        while(isUserExiting == false)
                        {
                          Console.WriteLine(""); //Empty line for increased readability
                          Console.WriteLine("Would you like to input another name? Yes - No or Exit");
                          decision = Console.ReadLine();
                          if (decision == "Yes")
                          {
                              Console.Write("Input name: ");
                              name = Console.ReadLine();
                          }
                          else
                          {
                              Console.WriteLine("Name is " + name);
                              isUserExiting = true;
                              Console.ReadLine();
                          }
    
                          if(decision == "Exit")
                          { 
                               isUserExiting = true;
                          }
                        }
                    }
                }
            }
    ya guess i should of proof read it i got side tracked
    Last edited by willmotil; February 19th, 2015 at 11:48 PM.

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

Featured