Home > Back-end >  Want my while loop to finish and output after I enter a number which is over 800 but it is giving me
Want my while loop to finish and output after I enter a number which is over 800 but it is giving me

Time:10-23

// While Loop & if statement

while (userInput < 800)
{
    if (userInput >0 && userInput <200)
    {
        cOne  ;
        sumLengthOne = sumLengthOne   userInput;
        userInput = int.Parse(Console.ReadLine());
    }  
    else if (userInput >200 && userInput <400)
    {
        cTwo  ;
        sumLengthTwo = sumLengthTwo   userInput;
        userInput = int.Parse(Console.ReadLine());
    }
    else if (userInput >400 && userInput <600)
    {
        cThree  ;
        sumLengthThree = sumLengthThree   userInput;
        userInput = int.Parse(Console.ReadLine());
    }
    else if (userInput >600 && userInput <800)
    {
        cFour  ;
        sumLengthFour = sumLengthFour   userInput;
        userInput = int.Parse(Console.ReadLine());
    }
    else if (userInput > 800)
    {
        break;
    }
}        

Console.WriteLine("| {0,-10} | {1,5} | {6,10} | {11,15} |" , "Range" , "Count" , "Sum Lengths" , "Percentage");
Console.WriteLine("==========");
Console.WriteLine("| {0,-10} | {1,5} | {6,10} | {11,15} |" , "<200"   "200 - 399"   "400 - 599");
Console.WriteLine("The Largest Bacteria Is: "   largestValue);
Console.WriteLine("The Smallest Bacteria Is: "   smallestValue);

The unwanted result is unhandled exception " Unhandled exception. System.FormatException: Index (zero based) must be greater than or equal to zero and less than the size of the argument list"

after the while loop I am outputting the results to console but it gives an unhandled exception instead.

CodePudding user response:

Im not sure how your userInput comes into play, from input field or from looping through values. but...

When inputs like 200, 400, 600, 800 are typed, you have no matching condition. you are either above or under 800 but not === 800

you conditions should be something like this:

        while (userInput < 800)
        {
            if (userInput >0 && userInput <= 200) // below or equal too
            {
                cOne  ;
                sumLengthOne = sumLengthOne   userInput;
                userInput = int.Parse(Console.ReadLine());
            }  
            else if (userInput >200 && userInput <= 400) // below or equal too
            {
                cTwo  ;
                sumLengthTwo = sumLengthTwo   userInput;
                userInput = int.Parse(Console.ReadLine());
            }
            else if (userInput >400 && userInput <= 600) // below or equal too
            {
                cThree  ;
                sumLengthThree = sumLengthThree   userInput;
                userInput = int.Parse(Console.ReadLine());
            }
            else if (userInput >600 && userInput <= 800) // below or equal too
            {
                cFour  ;
                sumLengthFour = sumLengthFour   userInput;
                userInput = int.Parse(Console.ReadLine());
            }
            else if (userInput > 800)
            {
                break;
            }
        }     
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe> Ofcourse if you want to break 200, 400, 600 & 800 and under you would have to do it the other way around - like this:

        while (userInput < 800)
        {
            if (userInput >= 0 && userInput <200) // 0 or above
            {
                cOne  ;
                sumLengthOne = sumLengthOne   userInput;
                userInput = int.Parse(Console.ReadLine());
            }  
            else if (userInput >= 200 && userInput <400) // 200 or above
            {
                cTwo  ;
                sumLengthTwo = sumLengthTwo   userInput;
                userInput = int.Parse(Console.ReadLine());
            }
            else if (userInput >= 400 && userInput <600) // 400 or above
            {
                cThree  ;
                sumLengthThree = sumLengthThree   userInput;
                userInput = int.Parse(Console.ReadLine());
            }
            else if (userInput >= 600 && userInput <800) // 600 or above
            {
                cFour  ;
                sumLengthFour = sumLengthFour   userInput;
                userInput = int.Parse(Console.ReadLine());
            }
            else if (userInput >= 800) // 800 or above
            {
                break;
            }
        }   
<iframe name="sif2" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

CodePudding user response:

In your line

Console.WriteLine("| {0,-10} | {1,5} | {6,10} | {11,15} |" , "Range" , "Count" , "Sum Lengths" , "Percentage");

That {6,10} doesn't mean "from char 6 to 10" but "use argument #6, formatted as 10 chars wide". And you don't have an argument #6 (or #11). You meant index 2 and 3 there.

Fixed:

Console.WriteLine("| {0,-10} | {1,5} | {2,10} | {3,15} |" , "Range" , "Count" , "Sum Lengths" , "Percentage");

Tip: for the following dividing line, use this:

Console.WriteLine(new string ('=', 33));

to output 33 "=" characters.

  • Related