Below is the code for addition of two numbers. I ask for user input and validate the same in the function - GetValidateInput(). Code is working fine, but is it correct approach to ask for user input from user-defined function or we should do this in Main() and only validate the data in user-defined function (so that this function can be reused for other validation in some other places). What is the right approach?
class Solution
{
static void Main(string[] args)
{
Console.WriteLine("Enter the First Number");
int firstNumber = GetValidateInput();
Console.WriteLine("Enter the Second Number");
int secondNumber = GetValidateInput();
int sum = SolveMeFirst(firstNumber, secondNumber);
Console.WriteLine("The Sum is {0}",sum);
Console.ReadLine();
}
static int SolveMeFirst(int firstNumber, int secondNumber)
{
return firstNumber + secondNumber;
}
static int GetValidateInput()
{
bool isValid = false;
string number = null;
int result;
do
{
number = Console.ReadLine();
if (int.TryParse(number, out result))
isValid = true;
else
Console.WriteLine("Invalid Number, please re-enter");
} while (!isValid);
return result;
}
}