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
 
Soldato
Joined
23 Feb 2009
Posts
4,978
Location
South Wirral
Change your getInput() method so that it doesn't return until a valid number is entered.

BTW: the break; statement inside the if() isn't needed - the loop will terminate anyway as you set isValid to false just before.
 
Associate
OP
Joined
11 Jul 2009
Posts
1,318
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:
Associate
Joined
4 Dec 2009
Posts
518
if (guess out of bounds) {
do error stuff
} else if (guess < randomNumber) {
do stuff
} else if (guess > randomNumber) {
do different stuff
} else {
its the same
}
 
Soldato
Joined
18 Jun 2010
Posts
6,575
Location
Essex
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
            {
                while (getInput() == false);

                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 bool getInput()
        {
            bool result = false;
            Console.WriteLine("Please enter number between 1 and 100: ", random.Next(10) + 1);
            answer = Console.ReadLine();

            if (int.TryParse(answer, out guess))
            {
                if (guess > 100)
                {
                    Console.WriteLine("Number too high, please enter a number between 1 and 100");
                }
                else
                {
                    Console.WriteLine("You entered: " + guess);
                    result  = true;
                }
            }
            else
            {
                Console.WriteLine("NOT A NUMBER");
            }
            return result;
        }
 
Soldato
Joined
28 Oct 2006
Posts
12,456
Location
Sufferlandria
I would do it with nested IFs rather than multiple IF + ELSEIF
Code:
if(guess is valid number)
{
    if(guess > 10)
    {
        if(guess <= answer)
       {
           if(guess == answer)
           {
               Console.WriteLine("Correct");
           }
           else
           {
               Console.WriteLine("Too low");
           }
       }
       else
      {
          Console.WriteLine("Too high");
      }
   }
   else
   {
      Console.WriteLine("Out of bounds");
   }
}
else
{
Console.WriteLine("Not valid number");
}

That means that if it fails one of the conditions, it's not going to go through and try to check all the others too.
 
Soldato
Joined
28 Oct 2006
Posts
12,456
Location
Sufferlandria
That wouldn't work because he needs to output different statements based on which condition fails. If you only do one IF statement and check every condition in it, you won't know which has failed and which message to output.
 
Associate
Joined
1 Jun 2014
Posts
1,574
With regards to input validation, I would complete that in your getInput() function either via recursion or a while loop and once you know you've got the correct type of input, move onto the logic for checking it against the answer.

Logic order is likely not problematic, but it should be achievable in 3 conditional statements as you have it. I'd likely do:

Code:
IF guess > answer:
    PRINT too high, guess again
ELSEIF guess < answer:
    PRINT too low, guess again
ELSE:
    PRINT Correct guess
 
Back
Top Bottom