I am very new to programming in general so I may be missing something very obvious. But still, which path leads to not returning a value?
public static int CheckAge()
{
int userAge;
bool inValidInput = true;
while (inValidInput)
{
Console.Write("Enter your age: ");
string stringAge = Console.ReadLine();
if (int.TryParse(stringAge, out userAge))
{
switch (userAge)
{
case int n when (n < 1 || n > 120):
Console.ForegroundColor = ConsoleColor.Red;
Console.Write("RANGE ");
Console.ForegroundColor = ConsoleColor.White;
Console.Write("error\n");
Console.Write("Accepted range are numbers (eg. 123) ");
Console.ForegroundColor = ConsoleColor.Red;
Console.Write("between 1 and 120\n");
Console.ForegroundColor = ConsoleColor.White;
break;
default:
return userAge;
}
}
else
{
Console.ForegroundColor = ConsoleColor.Magenta;
Console.Write("TYPE ");
Console.ForegroundColor = ConsoleColor.White;
Console.Write("error\n");
Console.Write("Accepted input types are ");
Console.ForegroundColor = ConsoleColor.Magenta;
Console.Write("numbers (eg. 123) ");
Console.ForegroundColor = ConsoleColor.White;
Console.Write("between 1 and 120\n");
continue;
}
}
}
When I look at it I feel like all paths should EVENTUALLY lead to:
default:
return userAge;
If I need to provide the full source code please let me know and I will add it, did not want to make it to messy in case all I needed was the example.
EDIT: I really liked the solution given by @oerkelens but I am struggling to make the userAge variable behave properly. Below Using:(return userAge; inside if statement):
public static int CheckAge()
{
int userAge;
bool inputIsValid = false;
while (!inputIsValid)
{
Console.Write("Enter your age: ");
string stringAge = Console.ReadLine();
if (!int.TryParse(stringAge, out userAge))
{
WriteNumericalError();
continue;
}
if (userAge < 1 || userAge > 120)
{
WriteRangeError();
continue;
}
inputIsValid = true;
return userAge;
}
}
Gives me the good old: 'Program.CheckAge()': not all code paths return a value
Below Using:(return userAge; outside if statement):
public static int CheckAge()
{
int userAge;
bool inputIsValid = false;
while (!inputIsValid)
{
Console.Write("Enter your age: ");
string stringAge = Console.ReadLine();
if (!int.TryParse(stringAge, out userAge))
{
WriteNumericalError();
continue;
}
if (userAge < 1 || userAge > 120)
{
WriteRangeError();
continue;
}
inputIsValid = true;
}
return userAge;
}
Solved the 'Program.CheckAge()': not all code paths return a value but introduces: Use of unassigned local variable 'userAge'
FINAL SOLUTION
public static int CheckAge()
{
int userAge = 0;
bool validInput = false;
while (!validInput)
{
Console.Write("Enter your age: ");
string stringAge = Console.ReadLine();
if (!int.TryParse(stringAge, out userAge))
{
WriteNumericalError();
continue;
}
if (userAge < 1 || userAge > 120)
{
WriteRangeError();
continue;
}
validInput = true;
}
return userAge;
}
private static void WriteNumericalError()
{
Console.ForegroundColor = ConsoleColor.Magenta;
Console.Write("TYPE ");
Console.ForegroundColor = ConsoleColor.White;
Console.Write("error\n");
Console.Write("Accepted input types are numbers ");
Console.ForegroundColor = ConsoleColor.Magenta;
Console.Write("(eg. 1 2 3 4 5 6 7 8 9 0)\n");
Console.ForegroundColor = ConsoleColor.White;
Console.WriteLine("Press [Enter] to try again");
Console.ReadLine();
}
private static void WriteRangeError()
{
Console.ForegroundColor = ConsoleColor.Red;
Console.Write("RANGE ");
Console.ForegroundColor = ConsoleColor.White;
Console.Write("error\n");
Console.Write("Your entered number does not fall within the accepted range ");
Console.ForegroundColor = ConsoleColor.Red;
Console.Write("1 to 5 for film choice - 1 to 120 for age input\n");
Console.ForegroundColor = ConsoleColor.White;
Console.WriteLine("Press [Enter] to try again");
Console.ReadLine();
}