sunflower
sunflower

Reputation: 531

Array printing wrong results C# Console

My code suppos to print an array of integers in wich the user enters as many elements he wants and enter -99 (as a string) to exit the loop.Also valid entries range is 0 - 10 with 0 and 10 included.I get wrong results when printing the array tha it prints the last entry only and zeros after .The code is in Console C#.Any help will be appreciated.Thanks.

namespace ExeNo1Ch7
{
class Program
{
    static void Main(string[] args)
    {
        int numOfEntry;
        int[] intNumbers = new int[100];

        numOfEntry = GetArrayOfIntegers(intNumbers);
        Console.WriteLine("\t\n You have entered  " + numOfEntry + " values " + "\t\n" + "They are:");
        for (int i = 0; i < numOfEntry; i++)
        {
            Console.WriteLine("\t\n" + intNumbers[i]);
        }

        Console.WriteLine("\t\n<< Press any key to Exit >> ");

        Console.ReadKey();
    }


    public static int GetArrayOfIntegers(int[] anArray)
    {
        string strValue;
        int counter = 0;
        Console.Write("\t\n Enter an enteger from 0 - 10 :");
        strValue = Console.ReadLine();

        for (int i = 0; i < anArray.Length; i++)
        {
            while (strValue != "-99")
            {
                anArray[i] = int.Parse(strValue);
                counter = counter + 1;
                if (anArray[i] >= 0 && anArray[i] <= 10)
                {
                    Console.Write("\t\n Enter an enteger from 0 - 10 :");
                    strValue = Console.ReadLine();                 
                }                 
                else
                {
                    Console.WriteLine("\t\n Please try again entering an integer in the range (0 - 10) only,");
                    Console.Write("\t\n Enter an enteger from 0 - 10 :");
                    strValue = Console.ReadLine();
                }
            }
        }

        return counter;
    }

Upvotes: 0

Views: 165

Answers (2)

Pierre-Luc Pineault
Pierre-Luc Pineault

Reputation: 9191

You have this behavior because you're overwriting the same value over and over in your while loop.

Basically, you have this :

for (i = 0; i < anArray.Length; i++)
{
    while (strValue != "-99")
    {
        anArray[i] = int.Parse(strValue);
    }
}

This means you will loop forever on the same i until the user inputs -99. You should drop the for loop and increment i in the while :

public static int GetArrayOfIntegers(int[] anArray)
{
    string strValue;
    int counter = 0;
    Console.Write("\t\n Enter an enteger from 0 - 10 :");
    strValue = Console.ReadLine();

    int i = 0;
    while (strValue != "-99")
    {
        anArray[i] = int.Parse(strValue);
        counter = counter + 1;
        if (anArray[i] >= 0 && anArray[i] <= 10)
        {
            Console.Write("\t\n Enter an enteger from 0 - 10 :");
            strValue = Console.ReadLine();
        }
        else
        {
            Console.WriteLine("\t\n Please try again entering an integer in the range (0 - 10) only,");
            Console.Write("\t\n Enter an enteger from 0 - 10 :");
            strValue = Console.ReadLine();
        }
        i++;
    }

There's a second problem; you assign the value before the range comparison. You should move the assignation and counter increment into the if block, like this :

public static int GetArrayOfIntegers(int[] anArray)
{
    string strValue;
    int counter = 0;

    Console.Write("\t\n Enter an enteger from 0 - 10 :");
    strValue = Console.ReadLine();
    while (strValue != "-99")
    {
        int value = int.Parse(strValue);
        if (value >= 0 && value <= 10)
        {
            anArray[counter] = value;
            counter = counter + 1;
            Console.Write("\t\n Enter an enteger from 0 - 10 :");
            strValue = Console.ReadLine();
        }
        else
        {
            Console.WriteLine("\t\n Please try again entering an integer in the range (0 - 10) only,");
            Console.Write("\t\n Enter an enteger from 0 - 10 :");
            strValue = Console.ReadLine();
        }
    }
    return counter;
}

Upvotes: 2

What you have is a for loop going through the input, but inside of that you also have a while loop going through the input, which is very awkward, and it looks like it would result in you being able to enter that -99 number 100 times.

instead of using both a for loop and a while loop, just && the clauses together in the for loop

    for (int i = 0; i < anArray.Length && strValue != "-99"; i++)
    {

you also should place a strValue = Console.ReadLine(); at the end of your for loop

Upvotes: 0

Related Questions