Search code examples
c#arraysuser-input

Why is my array only accepting the last number taken as the first element, and putting 0 as the rest of the elements?


The validation works as far as i had tested (that being all possible cases that could trigger the various parse checks), what I`m struggling with is inputing user input after its been validated to not be a character and within 0 - 100 as a double. The array takes the last input as the first element and sets the rest to 0.

I tried moving it and the end of the second major if, so outside the brackets but before the fail case at the last if statment. and the same result occured, beyond this I dont know what more I could change.

Note: the size of the array is gotten from within main.

private static void InputArray (params double [] number)
        {
            double d1;
            int i1 = 0;
            int k = 0;
            if (k < number.Length)
            {
                Write ("Enter a number ");
            Reset:
                input = ReadLine ();
                if (char.TryParse (input, out char c1))
                {
                    if ((c1 < -1) && (c1 > 11))
                    {
                        WriteLine ("The input: " + c1 + " is not an accepted input");
                        goto Reset;
                    }
                }
                if ((double.TryParse (input, out d1)) || (int.TryParse (input, out i1)))
                {
                    if (((d1 < 0.0) || (d1 > 100.0)) || ((i1 < 0) || (i1 > 100)))
                    {
                        WriteLine ("Input is outside of the range of valid inputs.");
                        goto Reset;
                    }
                    else
                        goto end;
                }
                else if (!double.TryParse (input, out d1))
                {
                    WriteLine ("End of input successful.");
                }
            }
            end:
            for(int e = 0; e < number.Length;)
            {
                number [e] = double.Parse (input);
                e++;
            }
        }

Heres the method call from main for the above method

for (int i = 0; i < number.Length; i++)
            {
                InputArray (number);

            }

Solution

  • Are you simply trying to fill the entire array with numbers entered by the user between 0.0 and 100.0 inclusive?

    static String input;
    
    static void Main(string[] args)
    {
        double[] number = new double[10];
        InputArray(number);
        
        WriteLine("Array contents:");
        WriteLine(String.Join(", ", number));
    
        Write("Press any key to quit...");
        ReadKey();
    }
    
    private static void InputArray(double[] number)
    {
        double d1;
        for(int k=0; k<number.Length; k++)
        {
            bool valid = false;
            do
            {
                Write("Enter a number between 0.0 and 100.0: ");
                input = ReadLine();
                valid = double.TryParse(input, out d1) && (d1 >= 0.0) && (d1 <= 100.0);
                if (!valid)
                {
                    WriteLine("Input is invalid or outside the accepeted range.");
                }
            } while (!valid);
            number[k] = d1;
        }
    }
    

    Sample run:

    Enter a number between 0.0 and 100.0: tacos are great
    Input is invalid or outside the accepeted range.
    Enter a number between 0.0 and 100.0: -4
    Input is invalid or outside the accepeted range.
    Enter a number between 0.0 and 100.0: 100.1
    Input is invalid or outside the accepeted range.
    Enter a number between 0.0 and 100.0: 0
    Enter a number between 0.0 and 100.0: 0.0
    Enter a number between 0.0 and 100.0: 100
    Enter a number between 0.0 and 100.0: 100.0
    Enter a number between 0.0 and 100.0: 3.1415926
    Enter a number between 0.0 and 100.0: 50
    Enter a number between 0.0 and 100.0: 72.3
    Enter a number between 0.0 and 100.0: unicorns
    Input is invalid or outside the accepeted range.
    Enter a number between 0.0 and 100.0: 83.4
    Enter a number between 0.0 and 100.0: 92
    Enter a number between 0.0 and 100.0: 1.3
    Array contents:
    0, 0, 100, 100, 3.1415926, 50, 72.3, 83.4, 92, 1.3
    Press any key to quit...