I am writing a console application, that gives the user a numerical choice to pick from (1-3). I have my logic down correctly, so that if they select 1-3 it works, but will cycle back through the loop if they choose any number outside that range and write a string error message "Invalid Entry! Please select 1-3 only.".
The problem I am running into is that if you just hit enter, without entering anything at all, it errors out. Or if you submit an alpha character instead of a number, it also errors out. How do I write it so that null entries or string/char entries will not bug it out when it is looking for only an int entry?
Loading
centraPosted Nov 9, 2011, 9:26 AM
centraPosted Nov 9, 2011, 9:22 AM
VulpesPosted Nov 9, 2011, 5:04 AM
You can use Console.Readkey() to validate characters as they're typed in, rather than wait until enter is pressed and then validate the whole string. However, you'll find that doing that in a bomb-proof way is much more complicated than what you're doing now and most developers are therefore content to stick with the latter.
centraPosted Nov 8, 2011, 11:44 PM
Javeed M ShaikhPosted Nov 8, 2011, 11:05 PM
centraPosted Nov 8, 2011, 11:01 PM
here's the before code:
Console.WriteLine("Please choose a character Race: \n1)Human \n2)Elf \n3)Orc");
race = int.Parse(Console.ReadLine());
after code:
Console.WriteLine("Please choose a character Race: \n1)Human \n2)Elf \n3)Orc");
int.TryParse(Console.ReadLine(), out race);
beautiful. I have a few things to change, but that's the answer I was looking for.
@Sam: with just the int.TryParse, if the entry is null it states "Invalid Entry!..." as per my conditions, but it submits nothing as the answer and progresses on towards the next step. So potentially, you could create a character with out ever choosing a race it seems. I'll be looking into this next...
centraPosted Nov 8, 2011, 10:41 PM
centraPosted Nov 8, 2011, 10:39 PM
class Game
{
public static int health = 100, action = 100, magic = 100, race, prof;
public static int damage = 5, armor = 5, evade = 5;
public static string name, lastName;
public static void Main()
{
char recreate;
char goodName;
string chosenRace = "";
string chosenProf = "";
Console.BackgroundColor = ConsoleColor.DarkRed;
Console.Title = "Game Version 0";
#region description
do
{
do
{
Console.WriteLine("Please choose a character Race: \n1)Human \n2)Elf \n3)Orc");
race = int.Parse(Console.ReadLine());
if (race == 1)
{
chosenRace = "Human";
health += 25;
Console.WriteLine("You have chosen Human. Humans have a +25 health bonus.");
Console.WriteLine("Health = {0} \nAction = {1} \nMagic = {2}", health, action, magic);
}
else if (race == 2)
{
chosenRace = "Elf";
magic += 25;
Console.WriteLine("You have chosen Elf. Elves have a +25 magic bonus.");
Console.WriteLine("Health = {0} \nAction = {1} \nMagic = {2}", health, action, magic);
}
else if (race == 3)
{
chosenRace = "Orc";
action += 25;
Console.WriteLine("You have chosen Orc. Orcs have a +25 action bonus.");
Console.WriteLine("Health = {0} \nAction = {1} \nMagic = {2}", health, action, magic);
}
else
{
Console.WriteLine("Invalid Entry! Please choose either 1, 2, or 3.");
}
} while (race > 3);
#endregion.....
***edit: added the class level variables so it makes more sense looking at the snippet***
Javeed M ShaikhPosted Nov 8, 2011, 10:38 PM
centraPosted Nov 8, 2011, 10:33 PM
Sam HobbsPosted Nov 8, 2011, 10:22 PM
Other than that, I am not sure how to explain what I mean.
Javeed M ShaikhPosted Nov 8, 2011, 10:16 PM
Sam HobbsPosted Nov 8, 2011, 9:44 PM
I certainly recommend the code that Vulpes suggests that uses TryParse. There is one thing I would add to the code that Vulpes shows, and that is to use the return code from TryParse. If the return code is false, then you know the input is not valid. Something else you could do is to first check to ensure that there was data read; if the string that Console.ReadLine() returns is empty, then you know that the input is not good. I am nbot sure what TryParse does when the string is empty; I assume it will report an error but probably it will work better if you simply ignore the data when there is none but then issue an error message if TryParse cannot convert the data otherwise.
It would also be good to check if Console.ReadLine() returns null, indicating EOF. This is not likely to happen for this program, but it is a good thing to do anyway. So then the standard input could be redirected to a file. When executing the program nomally, I think you can cause EOF by using Ctrl-Z and that would be a reasonable alternative for determining the end of input.
centraPosted Nov 8, 2011, 6:43 PM
VulpesPosted Nov 8, 2011, 6:28 PM
centraPosted Nov 8, 2011, 5:40 PM
@Javeed, I have not tried regex. I am actually not familiar with that at all. I'm going to have to do some research on that, but looks like it might be the key.
@Vulpes, I tried something similar to that, but it still errors out when anything other than a number is entered.
I'll try to recreate the code off the top of my head... Please be understanding if there are any syntax errors below. I am but a novice programmer trying to learn, and I typed this all off the top of my head, so it may be different or wrong from what I actually have.
public static int race;
public static char create='';
Console.Writeline("Welcome to game version 0.0")
Console.Writeline("Please chose a race:\n 1) Human\n2) Elf\n3) Orc")
Console.Readline(Int.Parse());
do{
if (race == 1)
{
Console.Writeline("You have chosen Human");
}
else if (race == 2)
{
Console.Writeline("You have chosen Elf");
}
else if (race == 3)
{
Console.Writeline("You have chosen Orc")
}
else
{
Console.Writeline("Invalid Intry! Please entry 1, 2 or 3 only")
}
Console.Writeline("Do you wish to chose a different race? Y or N")
create = Console.Readline(ToUpper());
}while (create == Y)
Javeed M ShaikhPosted Nov 2, 2011, 2:31 PM
static void Main(string[] args)
{
string minput = Console.ReadLine();
Regex mregex = new Regex("^[1-3]+$");
MatchCollection mmatch = mregex.Matches(minput);
if (mmatch.Count == 0)
{
Console.WriteLine("error");
}
else
{
Console.WriteLine("ok");
}
}
VulpesPosted Nov 2, 2011, 1:54 PM
Javeed M ShaikhPosted Nov 2, 2011, 1:12 PM
"^[1-3]+$" --> untested.