I am confused about strings and int. and can’t validate a name to be without numbers and strange charchters. a-z and A-Z are fine. I understand the do while loop and use of a sentinel. I seen Regex on here but it doesn’t work in my code for some reason unknown to me. I would rather go with a simple solution that I can understand it. I have validation of int in my code working great, but validating the name gets bool, and int errors.
static void Main(string[] args)
{
int age;
double mileage;
string strInput, name;
bool isValid;
DisplayApplicationInformation();
DisplayDivider("Start Program");
Console.WriteLine();
DisplayDivider("Get Name");
strInput = GetInput("your name");
name = strInput;
Console.WriteLine("Your name is: " + name);
Console.WriteLine();
do
{
DisplayDivider("Get Age");
strInput = GetInput("your age");
isValid = int.TryParse(strInput, out age);
if (!isValid || (age <= 0))
{
isValid = false;
Console.WriteLine("'" + strInput + "' is not a valid age entry. Please retry...");
}
}while (!isValid);
Console.WriteLine("Your age is: " + age);
//age = int.Parse(strInput);
//Console.WriteLine("Your age is: " + age);
Console.WriteLine();
do
{
DisplayDivider("Get Mileage");
strInput = GetInput("gas mileage");
isValid = double.TryParse(strInput, out mileage);
if (!isValid || (mileage <= 0))
{
isValid = false;
Console.WriteLine("'" + strInput + "' is not a valid mileage entry. Please retry...");
}
} while (!isValid);
Console.WriteLine("Your age is: " + mileage);
//mileage = double.Parse(strInput);
//Console.WriteLine("Your car MPT is: " + mileage);
TerminateApplication();
}
Here is a Regex that works for what you want
Basically, it says, “Match start of the string (^ means start) and only letter till end of string ($ means end of string) and there must be 1 or more letters (that’s the +)”
Probably the easiest
Now, this is assuming of course that you don’t want “Firstname Lastname” because that would mean there is a space involved. Let me know if you need spaces for name separations.