Carl Potter

Carl Potter

  • NA
  • 6
  • 30.1k

readling input and adding value into single dimension array

Nov 14 2010 3:49 PM
Looked through forums could not find answer. I want to input the names of cast members from a show into an single dimension array. first input number of cast members in show then use a for loop or while loop to input the names into an array based on how many cast members there are then read them back out and display them. I have got what I thought might be a working piece of code but it has one error " Use of unassigned local variable 'castName' " which seems to partly stop it working, it asks for the number of cast members then just print to screen the number of cast members so if you enter 10 it print 0 to 9 to the screen and stops. Fairly new to c# so forgive the code below, if any one can point me in the right direction that would be great.


namespace read_value_into_array
{
    class Program
    {
        static void Main(string[] args)
        {
            int number = 1;
            int castNumber = 0;
            string[] castName;
            string castNameInput = null;

            Console.WriteLine("enter the number of cast members");
            castNumber = int.Parse(Console.ReadLine());// get the number of cast members

            while (number < castNumber)// check that castNumber is less than number
            {
                Console.WriteLine("enter cast member " + number + " name");// display the a number of cast member
                castNameInput = Console.ReadLine();// get cast member name place in castNameInput variable
                castName[number] = castNameInput;// put castNameInput into castname[number] array based on value of number
                Console.WriteLine(number);// write the value of number
                number = number + 1;// increment the value of number
            }

        }
    }
}

Answers (2)