Repeating the question if user input is empty in C# [closed]












2














I'm having a basic problem with my school code here. The code needs to ask user input as a console program, but I need it to repeat the question when entered an empty field before proceeding. So far I've tried a lot of while-loops, !isStringNullorEmpty, string.lengths, tried to create a function that checks it and some if-statements. I can't get it to work on either one of these. The program proceeds to the end all of the time.



System.Console.Write("Give first name");
String firstname = System.Console.ReadLine();

System.Console.Write("Give last name");
String lastname = System.Console.ReadLine();

System.Console.Write("Give date of birth");
DateTime = dt = DateTime.Parse(System.Console.ReadLine();









share|improve this question













closed as off-topic by Default, Gerhard Barnard, EdChum, Umair, marsze Nov 13 '18 at 13:21


This question appears to be off-topic. The users who voted to close gave this specific reason:


  • "Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers. See: How to create a Minimal, Complete, and Verifiable example." – Default, Gerhard Barnard, EdChum, Umair

If this question can be reworded to fit the rules in the help center, please edit the question.









  • 3




    You should post code with your try which contains loop. Then we can say you where is problem instead of writting whole code.
    – Erik Šťastný
    Nov 13 '18 at 8:04


















2














I'm having a basic problem with my school code here. The code needs to ask user input as a console program, but I need it to repeat the question when entered an empty field before proceeding. So far I've tried a lot of while-loops, !isStringNullorEmpty, string.lengths, tried to create a function that checks it and some if-statements. I can't get it to work on either one of these. The program proceeds to the end all of the time.



System.Console.Write("Give first name");
String firstname = System.Console.ReadLine();

System.Console.Write("Give last name");
String lastname = System.Console.ReadLine();

System.Console.Write("Give date of birth");
DateTime = dt = DateTime.Parse(System.Console.ReadLine();









share|improve this question













closed as off-topic by Default, Gerhard Barnard, EdChum, Umair, marsze Nov 13 '18 at 13:21


This question appears to be off-topic. The users who voted to close gave this specific reason:


  • "Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers. See: How to create a Minimal, Complete, and Verifiable example." – Default, Gerhard Barnard, EdChum, Umair

If this question can be reworded to fit the rules in the help center, please edit the question.









  • 3




    You should post code with your try which contains loop. Then we can say you where is problem instead of writting whole code.
    – Erik Šťastný
    Nov 13 '18 at 8:04
















2












2








2







I'm having a basic problem with my school code here. The code needs to ask user input as a console program, but I need it to repeat the question when entered an empty field before proceeding. So far I've tried a lot of while-loops, !isStringNullorEmpty, string.lengths, tried to create a function that checks it and some if-statements. I can't get it to work on either one of these. The program proceeds to the end all of the time.



System.Console.Write("Give first name");
String firstname = System.Console.ReadLine();

System.Console.Write("Give last name");
String lastname = System.Console.ReadLine();

System.Console.Write("Give date of birth");
DateTime = dt = DateTime.Parse(System.Console.ReadLine();









share|improve this question













I'm having a basic problem with my school code here. The code needs to ask user input as a console program, but I need it to repeat the question when entered an empty field before proceeding. So far I've tried a lot of while-loops, !isStringNullorEmpty, string.lengths, tried to create a function that checks it and some if-statements. I can't get it to work on either one of these. The program proceeds to the end all of the time.



System.Console.Write("Give first name");
String firstname = System.Console.ReadLine();

System.Console.Write("Give last name");
String lastname = System.Console.ReadLine();

System.Console.Write("Give date of birth");
DateTime = dt = DateTime.Parse(System.Console.ReadLine();






c# loops






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 13 '18 at 8:01









dunielduniel

111




111




closed as off-topic by Default, Gerhard Barnard, EdChum, Umair, marsze Nov 13 '18 at 13:21


This question appears to be off-topic. The users who voted to close gave this specific reason:


  • "Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers. See: How to create a Minimal, Complete, and Verifiable example." – Default, Gerhard Barnard, EdChum, Umair

If this question can be reworded to fit the rules in the help center, please edit the question.




closed as off-topic by Default, Gerhard Barnard, EdChum, Umair, marsze Nov 13 '18 at 13:21


This question appears to be off-topic. The users who voted to close gave this specific reason:


  • "Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers. See: How to create a Minimal, Complete, and Verifiable example." – Default, Gerhard Barnard, EdChum, Umair

If this question can be reworded to fit the rules in the help center, please edit the question.








  • 3




    You should post code with your try which contains loop. Then we can say you where is problem instead of writting whole code.
    – Erik Šťastný
    Nov 13 '18 at 8:04
















  • 3




    You should post code with your try which contains loop. Then we can say you where is problem instead of writting whole code.
    – Erik Šťastný
    Nov 13 '18 at 8:04










3




3




You should post code with your try which contains loop. Then we can say you where is problem instead of writting whole code.
– Erik Šťastný
Nov 13 '18 at 8:04






You should post code with your try which contains loop. Then we can say you where is problem instead of writting whole code.
– Erik Šťastný
Nov 13 '18 at 8:04














5 Answers
5






active

oldest

votes


















1














You could try



string firstName = null;
Console.WriteLine("Give first name");
while(string.IsNullOrWhiteSpace(firstName = Console.ReadLine()))
Console.WriteLine("OMG you had one job as a user of this application, to put in the right value!");


You can do the same for DateTime



Console.Write("Give date of birth");
while (!DateTime.TryParse(Console.ReadLine(),out var dob))
Console.Write("OMG you had one job as a user of this application, to put in the right value!");




Additional Resources



DateTime.TryParse Method




Converts the specified string representation of a date and time to its
DateTime equivalent and returns a value that indicates whether the
conversion succeeded.







share|improve this answer































    0














    you can write



       System.Console.Write("Give first name");
    String firstname = null;
    while(firstname==null || firstname==""){
    firstname = System.Console.ReadLine();
    }





    share|improve this answer





























      0














      Try this:



      String lastname = null;

      Console.Write("Give last name: ");

      while (!String.IsNullOrEmpty(lastname)) {
      lastname = System.Console.ReadLine();
      }





      share|improve this answer





























        0














        Try this



                    var firstname = string.Empty;
        var lastname = string.Empty;
        var dt = DateTime.MinValue;
        do
        {
        System.Console.Write("Give first name");
        firstname = System.Console.ReadLine();

        } while (string.IsNullOrEmpty(firstname));
        do
        {
        System.Console.Write("Give last name");
        lastname = System.Console.ReadLine();
        } while (string.IsNullOrEmpty(lastname));


        do
        {
        System.Console.Write("Give date of birth");
        dt = DateTime.Parse(System.Console.ReadLine());
        } while (dt != DateTime.MinValue);





        share|improve this answer





























          0














          try this



                  bool value = true;

          while (value==true)
          {
          System.Console.Write("Give first name");
          String firstname = System.Console.ReadLine();
          if (firstname == "")
          {
          value = false;
          break;
          }
          System.Console.Write("Give last name");
          String lastname = System.Console.ReadLine();
          if (lastname == "")
          {
          value = false;
          break;
          }
          System.Console.Write("Give date of birth");
          DateTime dt = DateTime.Parse(System.Console.ReadLine());
          if (dt.ToString()=="")
          {
          value = false;
          break;
          }
          }





          share|improve this answer






























            5 Answers
            5






            active

            oldest

            votes








            5 Answers
            5






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            1














            You could try



            string firstName = null;
            Console.WriteLine("Give first name");
            while(string.IsNullOrWhiteSpace(firstName = Console.ReadLine()))
            Console.WriteLine("OMG you had one job as a user of this application, to put in the right value!");


            You can do the same for DateTime



            Console.Write("Give date of birth");
            while (!DateTime.TryParse(Console.ReadLine(),out var dob))
            Console.Write("OMG you had one job as a user of this application, to put in the right value!");




            Additional Resources



            DateTime.TryParse Method




            Converts the specified string representation of a date and time to its
            DateTime equivalent and returns a value that indicates whether the
            conversion succeeded.







            share|improve this answer




























              1














              You could try



              string firstName = null;
              Console.WriteLine("Give first name");
              while(string.IsNullOrWhiteSpace(firstName = Console.ReadLine()))
              Console.WriteLine("OMG you had one job as a user of this application, to put in the right value!");


              You can do the same for DateTime



              Console.Write("Give date of birth");
              while (!DateTime.TryParse(Console.ReadLine(),out var dob))
              Console.Write("OMG you had one job as a user of this application, to put in the right value!");




              Additional Resources



              DateTime.TryParse Method




              Converts the specified string representation of a date and time to its
              DateTime equivalent and returns a value that indicates whether the
              conversion succeeded.







              share|improve this answer


























                1












                1








                1






                You could try



                string firstName = null;
                Console.WriteLine("Give first name");
                while(string.IsNullOrWhiteSpace(firstName = Console.ReadLine()))
                Console.WriteLine("OMG you had one job as a user of this application, to put in the right value!");


                You can do the same for DateTime



                Console.Write("Give date of birth");
                while (!DateTime.TryParse(Console.ReadLine(),out var dob))
                Console.Write("OMG you had one job as a user of this application, to put in the right value!");




                Additional Resources



                DateTime.TryParse Method




                Converts the specified string representation of a date and time to its
                DateTime equivalent and returns a value that indicates whether the
                conversion succeeded.







                share|improve this answer














                You could try



                string firstName = null;
                Console.WriteLine("Give first name");
                while(string.IsNullOrWhiteSpace(firstName = Console.ReadLine()))
                Console.WriteLine("OMG you had one job as a user of this application, to put in the right value!");


                You can do the same for DateTime



                Console.Write("Give date of birth");
                while (!DateTime.TryParse(Console.ReadLine(),out var dob))
                Console.Write("OMG you had one job as a user of this application, to put in the right value!");




                Additional Resources



                DateTime.TryParse Method




                Converts the specified string representation of a date and time to its
                DateTime equivalent and returns a value that indicates whether the
                conversion succeeded.








                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Nov 13 '18 at 10:39

























                answered Nov 13 '18 at 8:06









                TheGeneralTheGeneral

                28k63365




                28k63365

























                    0














                    you can write



                       System.Console.Write("Give first name");
                    String firstname = null;
                    while(firstname==null || firstname==""){
                    firstname = System.Console.ReadLine();
                    }





                    share|improve this answer


























                      0














                      you can write



                         System.Console.Write("Give first name");
                      String firstname = null;
                      while(firstname==null || firstname==""){
                      firstname = System.Console.ReadLine();
                      }





                      share|improve this answer
























                        0












                        0








                        0






                        you can write



                           System.Console.Write("Give first name");
                        String firstname = null;
                        while(firstname==null || firstname==""){
                        firstname = System.Console.ReadLine();
                        }





                        share|improve this answer












                        you can write



                           System.Console.Write("Give first name");
                        String firstname = null;
                        while(firstname==null || firstname==""){
                        firstname = System.Console.ReadLine();
                        }






                        share|improve this answer












                        share|improve this answer



                        share|improve this answer










                        answered Nov 13 '18 at 8:06









                        HarisHaris

                        679




                        679























                            0














                            Try this:



                            String lastname = null;

                            Console.Write("Give last name: ");

                            while (!String.IsNullOrEmpty(lastname)) {
                            lastname = System.Console.ReadLine();
                            }





                            share|improve this answer


























                              0














                              Try this:



                              String lastname = null;

                              Console.Write("Give last name: ");

                              while (!String.IsNullOrEmpty(lastname)) {
                              lastname = System.Console.ReadLine();
                              }





                              share|improve this answer
























                                0












                                0








                                0






                                Try this:



                                String lastname = null;

                                Console.Write("Give last name: ");

                                while (!String.IsNullOrEmpty(lastname)) {
                                lastname = System.Console.ReadLine();
                                }





                                share|improve this answer












                                Try this:



                                String lastname = null;

                                Console.Write("Give last name: ");

                                while (!String.IsNullOrEmpty(lastname)) {
                                lastname = System.Console.ReadLine();
                                }






                                share|improve this answer












                                share|improve this answer



                                share|improve this answer










                                answered Nov 13 '18 at 8:07









                                SysDragonSysDragon

                                7,610143870




                                7,610143870























                                    0














                                    Try this



                                                var firstname = string.Empty;
                                    var lastname = string.Empty;
                                    var dt = DateTime.MinValue;
                                    do
                                    {
                                    System.Console.Write("Give first name");
                                    firstname = System.Console.ReadLine();

                                    } while (string.IsNullOrEmpty(firstname));
                                    do
                                    {
                                    System.Console.Write("Give last name");
                                    lastname = System.Console.ReadLine();
                                    } while (string.IsNullOrEmpty(lastname));


                                    do
                                    {
                                    System.Console.Write("Give date of birth");
                                    dt = DateTime.Parse(System.Console.ReadLine());
                                    } while (dt != DateTime.MinValue);





                                    share|improve this answer


























                                      0














                                      Try this



                                                  var firstname = string.Empty;
                                      var lastname = string.Empty;
                                      var dt = DateTime.MinValue;
                                      do
                                      {
                                      System.Console.Write("Give first name");
                                      firstname = System.Console.ReadLine();

                                      } while (string.IsNullOrEmpty(firstname));
                                      do
                                      {
                                      System.Console.Write("Give last name");
                                      lastname = System.Console.ReadLine();
                                      } while (string.IsNullOrEmpty(lastname));


                                      do
                                      {
                                      System.Console.Write("Give date of birth");
                                      dt = DateTime.Parse(System.Console.ReadLine());
                                      } while (dt != DateTime.MinValue);





                                      share|improve this answer
























                                        0












                                        0








                                        0






                                        Try this



                                                    var firstname = string.Empty;
                                        var lastname = string.Empty;
                                        var dt = DateTime.MinValue;
                                        do
                                        {
                                        System.Console.Write("Give first name");
                                        firstname = System.Console.ReadLine();

                                        } while (string.IsNullOrEmpty(firstname));
                                        do
                                        {
                                        System.Console.Write("Give last name");
                                        lastname = System.Console.ReadLine();
                                        } while (string.IsNullOrEmpty(lastname));


                                        do
                                        {
                                        System.Console.Write("Give date of birth");
                                        dt = DateTime.Parse(System.Console.ReadLine());
                                        } while (dt != DateTime.MinValue);





                                        share|improve this answer












                                        Try this



                                                    var firstname = string.Empty;
                                        var lastname = string.Empty;
                                        var dt = DateTime.MinValue;
                                        do
                                        {
                                        System.Console.Write("Give first name");
                                        firstname = System.Console.ReadLine();

                                        } while (string.IsNullOrEmpty(firstname));
                                        do
                                        {
                                        System.Console.Write("Give last name");
                                        lastname = System.Console.ReadLine();
                                        } while (string.IsNullOrEmpty(lastname));


                                        do
                                        {
                                        System.Console.Write("Give date of birth");
                                        dt = DateTime.Parse(System.Console.ReadLine());
                                        } while (dt != DateTime.MinValue);






                                        share|improve this answer












                                        share|improve this answer



                                        share|improve this answer










                                        answered Nov 13 '18 at 8:13









                                        Gaurav MoolaniGaurav Moolani

                                        9519




                                        9519























                                            0














                                            try this



                                                    bool value = true;

                                            while (value==true)
                                            {
                                            System.Console.Write("Give first name");
                                            String firstname = System.Console.ReadLine();
                                            if (firstname == "")
                                            {
                                            value = false;
                                            break;
                                            }
                                            System.Console.Write("Give last name");
                                            String lastname = System.Console.ReadLine();
                                            if (lastname == "")
                                            {
                                            value = false;
                                            break;
                                            }
                                            System.Console.Write("Give date of birth");
                                            DateTime dt = DateTime.Parse(System.Console.ReadLine());
                                            if (dt.ToString()=="")
                                            {
                                            value = false;
                                            break;
                                            }
                                            }





                                            share|improve this answer




























                                              0














                                              try this



                                                      bool value = true;

                                              while (value==true)
                                              {
                                              System.Console.Write("Give first name");
                                              String firstname = System.Console.ReadLine();
                                              if (firstname == "")
                                              {
                                              value = false;
                                              break;
                                              }
                                              System.Console.Write("Give last name");
                                              String lastname = System.Console.ReadLine();
                                              if (lastname == "")
                                              {
                                              value = false;
                                              break;
                                              }
                                              System.Console.Write("Give date of birth");
                                              DateTime dt = DateTime.Parse(System.Console.ReadLine());
                                              if (dt.ToString()=="")
                                              {
                                              value = false;
                                              break;
                                              }
                                              }





                                              share|improve this answer


























                                                0












                                                0








                                                0






                                                try this



                                                        bool value = true;

                                                while (value==true)
                                                {
                                                System.Console.Write("Give first name");
                                                String firstname = System.Console.ReadLine();
                                                if (firstname == "")
                                                {
                                                value = false;
                                                break;
                                                }
                                                System.Console.Write("Give last name");
                                                String lastname = System.Console.ReadLine();
                                                if (lastname == "")
                                                {
                                                value = false;
                                                break;
                                                }
                                                System.Console.Write("Give date of birth");
                                                DateTime dt = DateTime.Parse(System.Console.ReadLine());
                                                if (dt.ToString()=="")
                                                {
                                                value = false;
                                                break;
                                                }
                                                }





                                                share|improve this answer














                                                try this



                                                        bool value = true;

                                                while (value==true)
                                                {
                                                System.Console.Write("Give first name");
                                                String firstname = System.Console.ReadLine();
                                                if (firstname == "")
                                                {
                                                value = false;
                                                break;
                                                }
                                                System.Console.Write("Give last name");
                                                String lastname = System.Console.ReadLine();
                                                if (lastname == "")
                                                {
                                                value = false;
                                                break;
                                                }
                                                System.Console.Write("Give date of birth");
                                                DateTime dt = DateTime.Parse(System.Console.ReadLine());
                                                if (dt.ToString()=="")
                                                {
                                                value = false;
                                                break;
                                                }
                                                }






                                                share|improve this answer














                                                share|improve this answer



                                                share|improve this answer








                                                edited Nov 13 '18 at 8:44

























                                                answered Nov 13 '18 at 8:35









                                                Dhanushka DayawanshaDhanushka Dayawansha

                                                32619




                                                32619















                                                    Popular posts from this blog

                                                    Bressuire

                                                    Vorschmack

                                                    Quarantine