Search code examples
c#consoleconsole.writelineconsole.readline

Learning C#, wrote faulty program, need help as to why it's not function how I want it to


I'm new to coding and wrote this little console reading program to try and make sense of arrays, methods, etc.
I know my code is very faulty and probably not properly written (by book).
I would like some help on how to fix my program though, it has an issue where the console lets me input a number but it doesn't read from my choices method. Any help would be greatly valued.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace PalacioGameImproved
{
class Program
{
    static void Main(string[] args)
    {
        WelcomeMessage();
        choices();

    }


    public static string WelcomeMessage()
    {
        string writeLines;
        Console.WriteLine("Pick a DooWop");
        Console.WriteLine("{0,15}", "0 = Johnny");
        Console.WriteLine("{0,13}", "1 = Nick");
        Console.WriteLine("{0,15}", "2 = Conrad");
        Console.WriteLine("{0,14}", "3 = Diane");
        Console.WriteLine("{0,13}", "4 = Rick");
        writeLines = Console.ReadLine();
        return writeLines;
    }

    public static void choices()
    {


        string[] names = new string[5];
        names[0] = "Johnny";
        names[1] = "Nick";
        names[2] = "Conrad";
        names[3] = "Diane";
        names[4] = "Rick";

        string UserInput = Console.ReadLine();

        if (UserInput == "0")
        {
            Console.WriteLine("is it the array");
        }

        else if (UserInput == "1")
        {
            Console.WriteLine(names[1]);
        }

        else if (UserInput == "2")
        {
            Console.WriteLine(names[2]);
        }

        else if (UserInput == "3")
        {
            Console.WriteLine(names[3]);
        }

        else if (UserInput == "4")
        {
            Console.WriteLine(names[4]);
        }
        else
        {
            Console.WriteLine("That was not one of the choices, please try again.");
            WelcomeMessage();
        }
    }
}

}


Solution

  • You use Console.ReadLine 2 times. .net Fiddle example Your code should be

    using System;
    
    public class Program
    {
        public static void Main()
        {
           WelcomeMessage();
           choices();
        }
    
        public static void WelcomeMessage()
        {
           Console.WriteLine("Pick a DooWop");
           Console.WriteLine("{0,15}", "0 = Johnny");
           Console.WriteLine("{0,13}", "1 = Nick");
           Console.WriteLine("{0,15}", "2 = Conrad");
           Console.WriteLine("{0,14}", "3 = Diane");
           Console.WriteLine("{0,13}", "4 = Rick");
       }
    
       public static void choices()
       {
          string[] names = new string[5];
          names[0] = "Johnny";
          names[1] = "Nick";
          names[2] = "Conrad";
          names[3] = "Diane";
          names[4] = "Rick";
    
          string UserInput = Console.ReadLine();
    
          if (UserInput == "0")
          {
             Console.WriteLine("is it the array");
          }
    
          else if (UserInput == "1")
          {
             Console.WriteLine(names[1]);
          }
    
          else if (UserInput == "2")
          {
             Console.WriteLine(names[2]);
          }
    
          else if (UserInput == "3")
          {
             Console.WriteLine(names[3]);
          }
    
          else if (UserInput == "4")
          {
              Console.WriteLine(names[4]);
          }
          else
          {
              Console.WriteLine("That was not one of the choices, please try again.");
              WelcomeMessage();
          }
       }
    }