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 ?
Any ideas ? thanks
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