Jump to content
Search In
  • More options...
Find results that contain...
Find results in...

keeping user input under 100

Recommended Posts

Posted · Original PosterOP

hi i am still learning c# and i am developing a peice of code that will average the users input. i am struggling however witth keeping the user input under or equal to 100.


            int[] array1 = new int[15];

            Console.WriteLine("please give me 15 numbers and i will give you the average of them");
            for (int i = 0; i < array1.Length; i++)
                while (int.TryParse(Console.ReadLine(), out array1[i]) == false)
                    Console.WriteLine("Please give me a number not text");

          //here the averaging takes place it basscially averages the array that it has and here it joins all the array inputs together with a + sign
            Console.WriteLine("The average is {0} / 15 = {1}", string.Join("+", array1), array1.Average());



Link to post
Share on other sites

So you want the user to fill in a number, but if it's above 100, you want it to tell them it's not allowed and have them fill a new number in?

I would probably handle it in another way, assume user input is bad and only when it has been proven on all fronts that the input is good, you let it pass.


Earlier this week I helped you out a bit on another thread and I had actually written out the code needed. I had it have a while loop, that just went on and on if the input was not 'good' and had a simple function which just checked if the input was good.

The while loop looked like this:

bool inputGoodOne = false;
	// Can be shortened to while(!inputGoodOne)
	while(inputGoodOne == false)
		// ask for input
		string input = Console.ReadLine();
		// check input
		inputGoodOne = isInputGood(input);
			inputOne = Int32.Parse(input);
			Console.WriteLine("Please use only numbers, between 0 and 100");

And the function to check the input looks like this:

private bool isInputGood(string input)
	bool inputIsGood = false;
	// Check if it is a number and also if it's between 0 and 100
	if(int.TryParse(input, out int number))
		if((number >= 0) && (number <= 100))
			inputIsGood = true;

	return inputIsGood;


This would probably be the simplest way to check and repeat asking for input if the input was deemed incorrect..

be sure to read and understand the code and not just copy-paste it.

"those times will never come back :("

"The greatest part of the brain is memory, the worst thing that comes from memory is nostalgia. We remember but we can never go back!"


"There's no such thing as perfect. You're beautiful as you are Courage. With all your imperfections you can do anything." - Bathtub Barracuda

"Wish i could find a way to stop the pain.         -         Besides the one that i keep thinking of."

My gear:                    



The brains: AMD Ryzen 7 1700 ||

The torso: MSI X370 Gaming Pro Carbon ||

Short term memory: 2x8GB G.Skill TridentZ RGB 3200Mhz ||

Pixel pusher: EVGA RTX 2070 Super XC Gaming || 

Where I store memes: Samsung 960 EVO 250GB + Seagate Barracuda 2TB + Samsung 860 EVO 1TB || 

Un-heater: Be Quiet! Dark Rock 3 ||

Carboard box: Thermaltake Core G21 TG ||

Power squid: Corsair RM750x ||



Sound helmet: Beyerdynamic DT-990 250 Ohm ||

Clickity-clack (sadly not clicky :() : Coolermaster Masterkeys Pro S RGB ||

Return the (drawing) slab! : Wacom Intuos Pen Small (2015) ||

Rodent: Asus ROG Gladius ||

Cloth for rodent: CM Storm Swift-RX XXL ||


Old PC:

CPU: AMD Athlon X2 255 || Motherboard: ASUS M4A89GTD PRO || RAM: 4x2GB DDR3 || GPU: Nvidia Quadro 512MB || Case: Antec Sonata III || PSU: Antec Earthwatts 500W Bronze

Setup: Current setup

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now