Question: I am making a program that continually prompts the user to input numbers which are then stored in an array until the user provides a given response (E.g "continue"), where the program then displays the array in terminal.

I have been debugging my program and it is setting all values within the array to the last value from console input. 

How can I stop the counter from going all the way to the max for that array and instead step through the array 1 by 1 and input a new value from console each time

```
for (int i = 0; i < inputArray.GetLength(0); i++)
inputArray[i] = Convert.ToDouble(input);
goto STARTOVER;

```

Here is my main code:

```
using System;

namespace Task3
{
    class MainClass
{
    public static void Main(string[] args)
    {
        double[] inputArray = new double[40];

        Console.WriteLine("Begin");
        while (true)
        {
            STARTOVER:
            var input = Console.ReadLine();
            if (input.Equals("exit", StringComparison.OrdinalIgnoreCase))
            {
                break;
            }

            if (input.Equals("continue",StringComparison.OrdinalIgnoreCase))
            {
                goto DISPLAY;
            }

            else 
            {
                    for (int i = 0; i < inputArray.GetLength(0); i++)
                    inputArray[i] = Convert.ToDouble(input);
                    goto STARTOVER;
            }

        }
        DISPLAY:
        for (int i = 0; i < inputArray.GetLength(0); i++)
        {
            Console.Write("{0} ", inputArray[i]);
            Console.WriteLine();
        }

    }
}
}

```

From my understanding, I shouldn't use goto where possible but I cannot find an alternative to get the program to go back to where "STARTOVER:" is.

Answer: 
Aside from `goto`, which is pretty bad, the problem is the code loops through the entire array on every entry. It needs an integer variable that starts at 0 and increments with each new input, so it can set just that position each time.

```
public static void Main(string[] args)
{
    Console.WriteLine("Begin");
    double[] inputArray = new double[40];

    int inputCount = 0;      
    var input = Console.ReadLine();

    while (!input.Equals("exit", StringComparison.OrdinalIgnoreCase) 
        && !input.Equals("continue", StringComparison.OrdinalIgnoreCase)
        && inputCount < inputArray.Length)
    {
        inputArray[inputCount++] = Convert.ToDouble(input);  
        input = Console.ReadLine();       
    }

    for (int i = 0; i < inputCount; i++)
    {
        Console.Write("{0} ", inputArray[i]);
        Console.WriteLine();
    }
}

```

Even better to use a List:

```
public static void Main(string[] args)
{
    Console.WriteLine("Begin");
    var data = new List<double>;
    var input = Console.ReadLine();

    while (!input.Equals("exit", StringComparison.OrdinalIgnoreCase) 
        && !input.Equals("continue", StringComparison.OrdinalIgnoreCase))
    {
        data.Add(double.Parse((input));
        input = Console.ReadLine();       
    }

    for (double d in data)
    {
        Console.WriteLine(d);
    }
}

```
The key to repeatedly requesting input while a condition is met, is using a while loop

```
while(CurrentCondition != PassCondition)
{
   //do something, in this case ask for user input
}

```

a good idea would be to create methods, and call these methods where the goto methods are e.g 

```
    public static double[] inputArray = new double[40];
    public static string input;
    public static int idx = 0;

    static void Main(string[] args)
    {
        GetInput();
        Console.Read();
    }

    public static void GetInput()
    {
        Console.Write("enter a number to add to the array or 'continue' to display the array: ");
        input = Console.ReadLine();

        while (!CheckInput(input))
        {
            Console.Write("enter a number to add to the array or 'continue' to display the array: ");
            input = Console.ReadLine();
        }
    }

    public static bool CheckInput(string _input)
    {
        bool Continue = false;

        if (input == "continue")
        {
            PrintArray();
            Continue = true;
        }

        else
        {
            inputArray[idx] = double.Parse(_input);
            idx++;
            Continue = false;
        }

        return Continue;
    }

    public static void PrintArray()
    {
        for(int i = 0; i < inputArray.Length - 1; i++)
        {
            Console.WriteLine(inputArray[i]);
        }
    }
}

```