0

Okay, so I made this program for a small school project. It's just small Console Application were the user is supposed to enter 10 numbers and get the average result, the highest and lowest number. But I want to correct the user if he/she types in something that is not numeric. This is my code:

class Program
{
    static void Main(string[] args)
    {
        int num1, num2, num3, num4, num5, num6, num7, num8, num9, num10;

            Console.Write("Enter the 1st number: ");
            num1 = Convert.ToInt32(Console.ReadLine());

            Console.Write("Enter the 2nd number: ");
            num2 = Convert.ToInt32(Console.ReadLine());

            Console.Write("Enter the 3rd number: ");
            num3 = Convert.ToInt32(Console.ReadLine());

            Console.Write("Enter the 4th number: ");
            num4 = Convert.ToInt32(Console.ReadLine());

            Console.Write("Enter the 5th number: ");
            num5 = Convert.ToInt32(Console.ReadLine());

            Console.Write("Enter the 6th number: ");
            num6 = Convert.ToInt32(Console.ReadLine());

            Console.Write("Enter the 7th number: ");
            num7 = Convert.ToInt32(Console.ReadLine());

            Console.Write("Enter the 8th number: ");
            num8 = Convert.ToInt32(Console.ReadLine());

            Console.Write("Enter the 9th number: ");
            num9 = Convert.ToInt32(Console.ReadLine());

            Console.Write("Enter the 10th number: ");
            num10 = Convert.ToInt32(Console.ReadLine());

            int[] numbers = { num1, num2, num3, num4, num5, num6, num7, num8, num9, num10 };
            int biggestNumber = numbers.Max();
            int smallestNumber = numbers.Min();
            Console.WriteLine("Your biggest number was: " + biggestNumber);
            Console.WriteLine("Your smallest number was: " + smallestNumber);

            int result = (num1 + num2 + num3 + num4 + num5 + num6 + num7 + num8 + num9 + num10) / 10;
            Console.WriteLine("The average of your numbers are: " + result);

            Console.ReadLine();
        }
    }
}

After researching online, I found this as a possible solution:

            Console.Write("Enter the 1st number: ");
            while (Int32.TryParse(Console.ReadLine(), out num1) == false)
               {
                Console.WriteLine("Type a number please.");
               }

Although this works, to do this for each of the entries (num2, num3, num4, and so on) all the way down the code. So I thought maybe it's a simpler way to this instead of copy pasting the code block for each user input.

My question is: Is there an easier / more simplified solution to validate all the user inputs (at once maybe)?

EDIT: If you can help me I would love to see how you would implement your solution in my code :)

2
  • 6
    Make an array an fill it in loop. That way you will have only one place for your logic. Commented Jan 7, 2016 at 20:42
  • 2
    I agree with @GiorgiNakeuri. It would also cut down on the number of variables. Commented Jan 7, 2016 at 20:42

3 Answers 3

1

You need something like that, man. Collect all values to List and work with it :

        List<int> numbers = new List<int>();
        while (numbers.Count() < 10)
        {
            Console.Write("Enter the " + (numbers.Count() + 1) + "st number: ");
            int num;
            while (Int32.TryParse(Console.ReadLine(), out num) == false)
            {
                Console.WriteLine("Type a number please.");
            }
            numbers.Add(num);
        }

        int biggestNumber = numbers.Max();
        int smallestNumber = numbers.Min();
        Console.WriteLine("Your biggest number was: " + biggestNumber);
        Console.WriteLine("Your smallest number was: " + smallestNumber);

        int result = numbers.Sum()/numbers.Count();
        Console.WriteLine("The average of your numbers are: " + result);

        Console.ReadLine();
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks, that's a cleaner, simpler and probably much better code then mine. :) Now I'm just going to make sure I understand all thats happening. Only had one class of C# and .net programming so far, so still pretty much a newb :)
Do not hesitate to ask me if you will have any questions. We all started from zero few time ago :)
Thanks :) Have had some bad experiences earlier when I was still in my first year of school so I always try extra hard to find the help I need online before I post a question anywhere. The only things I can't quite understand from this code yet, is what "TryParse" really does, but hopefully I'll understand more as I learn more in class. Also the tooltips in Visual Studio really helps :)
Try to investigate that link : dotnetperls.com/parse In two words : method return bool value. If parse succeed - true, else - false. "out num" = num will be filled if success by converted value. As a result we have infinite loop using while and try and try and try to convert new and new and new entered value (using Console.ReadLine()). While loop will be stopped when value will be success parsed. And next we will add it to array.
0

This is an idea only, where basically you loop until the user enters a correct number ten times. When the input is correct, the number is saved in input[indexOfInput]

static void Main(string[] args)
{
    int[] input = new int[10];
    for (int i = 0; i < 10; i++)
    {
        Console.WriteLine("Write " + (i + 1) + " number");
        int temp;
        while (!int.TryParse(Console.ReadLine(), out temp)
        {
            Console.WriteLine("Write " + (i + 1) + " number");
        }
        input[i] = temp;
    }
    // at this point you have all 10 numbers written by the user
}

Comments

0

I would use a foreach loop but a while loop will also work.

Here is my solution

  var myList = new List<string>() { "1", "number2", "3", "4", "5", "6", "7", "8" };
  var numbers = new List<int>();

  foreach (var str in myList)
  {
    int myInt;
    if (!int.TryParse(str, out myInt))
      Console.WriteLine(string.Format("{0} is not a number. Type a number please.", str));
    else
      // Otherwise add the number to a new list of int's
      numbers.Add(myInt);        
  }

  // display the min / max vals only if there was not an error
  int biggestNumber = numbers.Max();
  int smallestNumber = numbers.Min();
  Console.WriteLine("Your biggest number was: " + biggestNumber);
  Console.WriteLine("Your smallest number was: " + smallestNumber);

  int result = numbers.Sum() / numbers.Count();
  Console.WriteLine("The average of your numbers are: " + result);

2 Comments

Thanks :) If I may ask, why would you prefer a foreach loop over a while loop?
Then you don't need to use the loop counter i... you can refer directly to the string in the list... i.e. you don't need to use myList[i] to get the string that you want to convert.

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.