Hello,
Right now i am trying to sharp (ha made a funny) my skills by bugging my program and fixing it, well my question is, lets say a user is prompt for only a number but accidently puts a letter in it, how do I go about that, or vise versa, I have the code below which works fine but if you can let me know what i am missing to fix that, it would be great.
Thanks in advance
Code:enum compuss:int { north = 1, south = 2, west = 3, east = 4, } static int input; static void Main(string[] args) { Console.WriteLine("Please enter \n1=north\n2=south\n3=west\n4=east\n"); input = int.Parse(Console.ReadLine()); while (input !=1 && input !=2 && input !=3 && input!=4) { Console.WriteLine("\nPlease enter 1, 2, 3, or 4"); input = int.Parse(Console.ReadLine()); } Console.WriteLine("\nyou have entered to go " + input + " direction " + (compuss)input); Console.ReadKey();
Oops, correct: how do i go about if a user inputs a string, or letter and not a number, the while loops kicks in, which works for any number however I am trying to figure out if someone puts in a string or different character, then loops takes affect? So far as shown i got the while loop down and using in many practices so i can keep it fresh in my mind, but of course string and int together is difficult.
You need to run an exception loop. Here is a tutorial on how to go about this: Exception Handling in .NET
Hello gaylo565,
Thank you for that link, i have read it and is very interesting, however, does it solve also command application issues too, or does it work only in OOP?
Hello Xav,
Is this a good example with the code I included above?
Because if i do try, catch, or final, then it would through this error message "invalid token 'try' in class, struct, or interface member declaration" and also "Type or namespace definition, or end-of-file expected". can you explain what I am doing wrong and/or correct the code? thanks for all your help, I am getting better... right?Code:try { enum compuss : int { north = 1, south = 2, west = 3, east = 4, } static int input; static void Main(string[] args) { Console.WriteLine("Please enter \n1=north\n2=south\n3=west\n4=east\n"); input = int.Parse(Console.ReadLine()); while (input != 1 && input != 2 && input != 3 && input != 4) { Console.WriteLine("\nPlease enter 1, 2, 3, or 4"); input = int.Parse(Console.ReadLine()); } Console.WriteLine("\nyou have entered to go " + input + " direction " + (compuss)input); Console.ReadKey(); } }
ok cool, thanks, but when i put the try { after the main like you said, the error message appears "Expected catch or finally" and i made sure to close the brackets.
yup that worked thanks : )
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks