1

Sorry i want to store specific input data/numbers in an array. In my array i want only the correct numbers to be stored and not the wrong once. I want the proram to continue as long as i have not gotten 4 correct number in my array. Then i want to print out the total number of array value. How can I do this in C#? Here is my code:

            int min = 5;
            int max = 10;
            int[] array = new int[4];
            int count = 0;

            for (int i = 0; i < array.Length; i++)
            {
            Console.WriteLine("enter btw 5 och 10");
            int val = int.Parse(Console.ReadLine());
            array[i] = val;

            if (val >= min && val <= max)
            {
                Console.WriteLine("Correct, continue...");
                count++;
                continue;
            }
            else
            {
                Console.WriteLine("wrong, enter btw 5 och 10");
                continue;
            }

            }
            Console.WriteLine(count);
            Console.ReadKey();
}

If someone enter e.g 11 or 2, the program will not count it. Thank you for the help.

3
  • Why an array? Why not a more-specialized collection type? Commented Mar 27, 2017 at 11:50
  • 1
    @rory.ap probably an assignment from school Commented Mar 27, 2017 at 11:52
  • What's vektor? Maybe it should be array? Commented Mar 27, 2017 at 11:54

2 Answers 2

1

Fungerar det?

{    
    var min = 5;
    var max = 10;
    var array = new int[4];
    var count = 0;
    var total = 0;

    for (int i = 0; i < array.Length; i++)
    {
        Console.WriteLine("Enter between 5 and 10:");
        var correct = false;

        while (!correct)
        {
            var val = int.Parse(Console.ReadLine());

            if (val >= min && val <= max)
            {
                Console.WriteLine("Right number...");
                array[i] = val; //Add the value to the array
                count++; //Increase count
                total += val; //Add the value to total
                correct = true; //Break the while loop
            }
            else
            {
                Console.WriteLine("Wrong, enter between 5 and 10:");
            }
        }
    }
    Console.WriteLine($"The total of the values are: {total}");
    Console.ReadKey();
}
Sign up to request clarification or add additional context in comments.

Comments

1

You could try something like:

int min = 5;
int max = 10;
int[] array = new int[4];
int index = 0;

while(true)
{
    Console.WriteLine("enter btw 5 och 10");
    int val = int.Parse(Console.ReadLine());

    if (val >= min && val <= max)
    {
        Console.WriteLine("Rätt siffra...");
        array[index] = val;

        if(index >= array.Length-1)
            break;

        index++;
    }
    else
    {
        Console.WriteLine("wrong, enter btw 5 och 10");
    }
}

Console.WriteLine(index);
Console.ReadKey();

It's even better to use int.TryParse() (like I would write it)

int min = 5;
int max = 10;
int[] array = new int[4];
int count = 0;

while(true)
{
    int val;

    // read the input
    Console.WriteLine("enter btw 5 och 10");
    string input = Console.ReadLine();

    // parse it
    if(!int.TryParse(input, out val))
    {
        Console.WriteLine("Not a valid number");
        continue;
    }

    // check the range
    if (val < min || val > max)
    {
        Console.WriteLine("wrong, enter btw 5 och 10");
        continue;
    }

    // store it.
    Console.WriteLine("Rätt siffra...");
    array[count] = val;

    // are we done?
    if(count== array.Length-1)
        // yes, break the while loop before we increase.
        break;

    // increase array index
    count++;
}

Console.WriteLine(count);
Console.ReadKey();

2 Comments

Original question could be improved, adding some integer validation, either try-catch or Int32.TryParse()
Thanks for your comment

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.