Problem with if statment

Associate
Joined
11 Jul 2009
Posts
1,318
Hi

I'm having a problem with my if statment in that if the user enters an letter rather than a number it will display the error message but also display "Too low" is there anyway to stop this from happening ?

Code:
public static bool isValid = true;
        public static Random random = new Random();
        public static int randomNumber = random.Next(10);
        public static int guess;
        public static String answer;

        static void Main(string[] args)
        {
            CashCall();
            
        }

        public static void CashCall()
        {
            do
            {
                getInput();

                if (guess == randomNumber)
                {
                    Console.WriteLine("*****RIGHT******");
                    Console.WriteLine("Cash call number is " + randomNumber);
                    isValid = false;
                    break;
                }
                 else if (guess > randomNumber)
                {
                    Console.WriteLine("Wrong too high");

                }
                 else
                {
                    Console.WriteLine("TOO LOW");
                }


            } while (isValid == true);
        }

        public static void getInput()
        {
            Console.WriteLine("Please enter number between 1 and 100: ", random.Next(10) + 1);
            answer = Console.ReadLine();

            if (int.TryParse(answer, out guess))
            {
                Console.WriteLine("You entered: " + guess);

            }
            else
            {
                Console.WriteLine("NOT A NUMBER");
            }
        }

Any ideas ? thanks
 
Thanks!

If I wanted to add in the following validation

Code:
if(guess > 10)
                    {
                        Console.WriteLine("OUT OF BOUNDS");
                    }

Where would I put so that only that message prints when the guess is great than 10? atm it prints the guess is out of bounds but also prints out too high.
 
Last edited:
Back
Top Bottom