I'm currently self-teaching myself C# and have had a pretty decent grasp on it, but I'm lost on how to validate that the user only enters one word answer and that it's capitalized otherwise I want to give them another chance to try.
This what I have so far:
static void Main(string[] args)
{
//assigned variable
string userInput;
//intializing empty string
string answerInput = string.Empty;
//Creating loop
while ((answerInput == string.Empty) || (answerInput == "-1"))
{
//This is asking the question to the user
Console.WriteLine("Enter your favorite animal: ");
//this is storing users input
userInput = Console.ReadLine();
//using function to validate response
answerInput = letterFunc(userInput);
}
}
//Creating function to only allow letters and making sure it's not left blank.
private static string letterFunc (string validate)
{
//intializing empty string
string returnString = string.Empty;
//validating it is not left empty
if(validate.Length > 0)
{
//iterating through the passed string
foreach(char c in validate)
{
//using the asciitable to validate they only use A-Z, a-z, and space
if ((((Convert.ToInt32(c)) > 64) && ((Convert.ToInt32(c)) < 91)) || (((Convert.ToInt32(c)) > 96) && ((Convert.ToInt32(c)) < 123)) || (Convert.ToInt32(c) == 32))
{
//appensing sanitized character to return string
returnString += c;
}
else
{
//If they try to enter a number this will warn them
Console.WriteLine("Invalid input. Use letters only.");
}
}
}
else
{
//If user enters a blank input, this will warn them
Console.WriteLine("You cannot enter a blank response.");
}
//returning string
return returnString;
}
I was wondering if it's possible to do it inside the function I created to validate they only use letters and that it isn't empty with a detailed explaination. Thanks.
I figured it out. Thanks everyone for trying to help.