Jump to content

keeping user input under 100

Daniboi

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 comment
Share on other sites

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);
		if(inputGoodOne)
		{
			inputOne = Int32.Parse(input);
		}
		else
		{
			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.

"We're all in this together, might as well be friends" Tom, Toonami.

 

mini eLiXiVy: my open source 65% mechanical PCB, a build log, PCB anatomy and discussing open source licenses: https://linustechtips.com/topic/1366493-elixivy-a-65-mechanical-keyboard-build-log-pcb-anatomy-and-how-i-open-sourced-this-project/

 

mini_cardboard: a 4% keyboard build log and how keyboards workhttps://linustechtips.com/topic/1328547-mini_cardboard-a-4-keyboard-build-log-and-how-keyboards-work/

Link to comment
Share on other sites

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

×