Click here to Skip to main content
15,892,005 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I need help on how I can allow the user to re-enter data that has been detected as invalid (not exit the program).

for example how can I make this to be re-entered and not exit. :
C#
 Console.Write("Enter Street Number >");
                    vtemp = Console.ReadLine();
                    street_number = vtemp;
                    if (string.IsNullOrEmpty(street_number))
                    {
                        Console.WriteLine("Please enter correct data");
                        return; 
}



Thanks
Posted

Of course you can request the same data in the loop until the user meets the input data constraints. That should answer your question.

However, it's pretty ugly and annoying to the users. Most cultured console applications are designed to be used without any interactive input. Instead, all the parameters are entered at once in a command line. If there are too many of them, one or more of the parameters could specify some kind of input file(s) where you put most of input parameters. (There are some notorious exclusions from this no-interactive rule of thumb though.)

If you what to go in this way, you can use my robust easy-to-use command line parsing utility:
Enumeration-based Command Line Utility[^].

You can use another more comprehensive (but perhaps less easy to use for a developer) library I referenced in my article.

Alternatively, you can think to provide more convenient user input through windowed UI, either with System.Windows.Forms or WPF library.

—SA
 
Share this answer
 
v3
Comments
malakas1821 20-Aug-12 2:57am    
can I use for Loop, while or do while Loop to do this ?
Sergey Alexandrovich Kryukov 20-Aug-12 3:10am    
Are you a developer or not?! The answer is -- whatever you want.
If in doubt, put some code using "Improve question" and let us help you to fix it if it does not work.
Or did I just wasted my time on you?

I told you -- don't do it, use command line. What prevents you from doing it?

And I just wonder if you voted 1? and, if so, why?

--SA
malakas1821 20-Aug-12 3:13am    
Im a programming student for 4 weeks.
Sergey Alexandrovich Kryukov 20-Aug-12 3:17am    
That was not my question... :-)

Just follow my advice and accept this answer formally (green button). If you show the code and needs help, we will gladly help you.
--SA
malakas1821 20-Aug-12 3:34am    
Sorry but I need to use for,while or do loop to do it.
Hi,

You can use do... while loop to do the same. you need to come out from the while loop when you are getting correct date, till you can ask user to enter correct data again and again.

But i prefer you to add some limit say 10 or 20 times to enter invalid data then application should be exit with appropriate message.

Thanks
-Amit Gajjar
 
Share this answer
 
v2
Comments
malakas1821 20-Aug-12 3:11am    
give me an example.
AmitGajjar 20-Aug-12 3:12am    
http://msdn.microsoft.com/en-us/library/370s1zax.aspx

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900