Okay I have created a validation loop which addresses the user entering a number between 10 and 50, as long as it is within these parameters it is supposed to ask for another number. When it is out of these parameters the program should let the user know it is outside the parameter and end the process. Below is what I have written, not sure where I went wrong as it always says it is a valid number, as long as the first number is valid.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Validator
{
    class Validator
    {
        public static void Main(string[] args)
        {
            //initialize variables
            String userInput = "";
            int userInfo = 0, intValue;

            //provide instructions to the user
            Console.WriteLine("Please enter a value between 10 and 50:");

            //start the read process
            userInput = Console.ReadLine();

            //parse the user input
            int numValue;
            bool parsed = Int32.TryParse(userInput, out numValue);

            //while loop parameters
            while ((numValue >= 10) && (numValue <= 50))
            {
                intValue = Convert.ToInt32(userInfo);
                userInfo += intValue;
                Console.WriteLine("Your Number is within the parameters, enter another number.");
                userInput = Console.ReadLine();
            }
            
            //end of program lines printed once number is outside the range
            Console.WriteLine("The Number you have entered is outside the parameters");
            Console.Read();
        }
    }
}

Any assistance is appreciated, thanks.

I think the problem lies here:

intValue = Convert.ToInt32(userInfo);
userInfo += intValue;

intValue has no value specified and you set userInfo to zero at the start. So you are really saying intValue = 0, userInfo += inValue (which is 0 += 0).
I think you should be using userInput not userInfo.

That's exactly what is going to happen. You forgot to update numValue inside the while loop. It's stuck at the first value that entered before the loop. Also, the while loop is going to run as long as numValue is between 10 and 50. You need a NOT (!) before your evaluation expression (e.g. !(x==y))

In your case:

!((numValue >= 10) && (numValue <= 50))

BTW, you can improve this code by eliminating the out-of-loop entry of the first value by using do..while.

do
{
   prompt user for input
   convert to int32
}  while (user input not between 10 and 50 )

Good luck,
Bill

That's exactly what is going to happen. You forgot to update numValue inside the while loop. It's stuck at the first value that entered before the loop. Also, the while loop is going to run as long as numValue is between 10 and 50. You need a NOT (!) before your evaluation expression (e.g. !(x==y))

In your case:

!((numValue >= 10) && (numValue <= 50))

BTW, you can improve this code by eliminating the out-of-loop entry of the first value by using do..while.

do
{
   prompt user for input
   convert to int32
}  while (user input not between 10 and 50 )

Good luck,
Bill

PERFECT! Thank you. I will update this.

This code is set to always show the user an error message at the bottom.

It would be better to evaluate the value as soon as the user enters it and "return" immediately if it's not in the right range.

An alternative would be to loop until the program receives a number in the right range.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.