Click here to Skip to main content
13,091,300 members (52,892 online)
Rate this:
Please Sign up or sign in to 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. :
 Console.Write("Enter Street Number >");
                    vtemp = Console.ReadLine();
                    street_number = vtemp;
                    if (string.IsNullOrEmpty(street_number))
                        Console.WriteLine("Please enter correct data");

Posted 19-Aug-12 20:43pm
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

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.

George Tsoukatos 20-Aug-12 2:57am
can I use for Loop, while or do while Loop to do this ?
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?

@amitgajjar 20-Aug-12 3:11am
good answer 5+, even i prefer Window application / WPF instead of command line utility.
George Tsoukatos 20-Aug-12 3:13am
Im a programming student for 4 weeks.
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.
Thank you, Amit.
The choice of console utility vs Forms/WPF/Metro/... is not only the matter of preference but the class of the problem and general style. Some utilities are needed to be used in other console-based work, such as batch, builds, etc...
By the way, command line does not mean console-only, is used an absolutely all applications...
@amitgajjar 20-Aug-12 3:24am
ok, thanks for the information.
George Tsoukatos 20-Aug-12 3:34am
Sorry but I need to use for,while or do loop to do it.
Rate this: bad
Please Sign up or sign in to vote.

Solution 2


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.

-Amit Gajjar
George Tsoukatos 20-Aug-12 3:11am
give me an example.
@amitgajjar 20-Aug-12 3:12am

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

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web04 | 2.8.170813.1 | Last Updated 20 Aug 2012
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100