i encountered an error "Use of unassigned variable 'attempt'" when i run the code below, i don't understand the reason because as i understand the try
statement block always runs so the variable should be assigned by the user ? Or am i wrong ? If anyone has i fix or work around that would be helpful.
static void MagicNumber(int rndMin, int rndMax, int lives = 4)
{
Random rnd = new Random();
int rndNum = rnd.Next(rndMin, rndMax);
int attempt;
do
{
try
{
Console.WriteLine($"Remaining lives : {lives}");
Console.WriteLine($"Enter a number between {rndMin} and {rndMax} :");
attempt = int.Parse(Console.ReadLine());
}
catch
{
Console.WriteLine("Error : Enter a number !");
}
} while ((lives > 0) || (attempt < rndMin) || (attempt > rndMax));
}
MagicNumber(1, 40, 5);