Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# Application
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");
                        return; 
}
 

Thanks
Posted 19-Aug-12 21:43pm
Rate this: bad
good
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.
 
—SA
  Permalink  
v3
Comments
George Tsoukatos at 20-Aug-12 2:57am
   
can I use for Loop, while or do while Loop to do this ?
Sergey Alexandrovich Kryukov at 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
George Tsoukatos at 20-Aug-12 3:13am
   
Im a programming student for 4 weeks.
Sergey Alexandrovich Kryukov at 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
George Tsoukatos at 20-Aug-12 3:34am
   
Sorry but I need to use for,while or do loop to do it.
@amitgajjar at 20-Aug-12 3:11am
   
good answer 5+, even i prefer Window application / WPF instead of command line utility.
Sergey Alexandrovich Kryukov at 20-Aug-12 3:21am
   
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...
--SA
@amitgajjar at 20-Aug-12 3:24am
   
ok, thanks for the information.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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
  Permalink  
v2
Comments
George Tsoukatos at 20-Aug-12 3:11am
   
give me an example.
@amitgajjar at 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)

  Print Answers RSS
0 OriginalGriff 8,149
1 Sergey Alexandrovich Kryukov 7,287
2 DamithSL 5,614
3 Manas Bhardwaj 4,986
4 Maciej Los 4,910


Advertise | Privacy | Mobile
Web02 | 2.8.1411023.1 | Last Updated 20 Aug 2012
Copyright © CodeProject, 1999-2014
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