3

I am stuck on a c# issue where I cant get the while loop to end.

userValue is a string that is input earlier on in the program.

The string is formatted in DateTime, and the new input need to match the original userValue. I.E Confirming the the users original birth date.

I used a try / catch to determine if the input can be parsed to DateTime.

Currently it is just a constant loop, i'm trying to have the loop stop once the users birth date is verified.

string userValueTwo;

int stop = 0;
while (stop != 0)
{
    Console.WriteLine("Please verify your birth date");
    userValueTwo = Console.ReadLine();

     try
     {
        DateTime birthdayTwo = DateTime.Parse(userValueTwo);
     }
     catch
     {
         Console.WriteLine("You did not enter a valid format.");
         Console.ReadLine();
     }

     if (userValueTwo == userValue)
     {
         Console.WriteLine("Birth date confirmed.");
         Console.ReadLine();
     }

    else 
    {
        Console.WriteLine("Your birthday did not match our records. Please try again");
        Console.ReadLine();
    }
}
4
  • 3
    You never increment stop or call break so it can't exit the loop. Commented Oct 13, 2015 at 20:27
  • 1
    Consider using DateTime.TryParse instead of using an Exception to capture the failure of parsing. Commented Oct 13, 2015 at 20:33
  • On the other hand... can it even enter the loop? stop = 0; then stop != 0 should always result in false if there's nothing in between. Commented Oct 13, 2015 at 20:33
  • @Aaron As Dave pointed out, and as you see in Kyriacross's answer, you are in direct control of when (or if ever) a while loop ends. You can either terminate it "early" with the break; command or let the while loop know that the condition has been met to terminate. In general, while (condition==true) { this.do(something); } loops continue forever until your condition becomes false. That won't happen arbitrarily; you need to set that condition to false, somehow. Commented Oct 13, 2015 at 20:37

4 Answers 4

5

You may use the break statement after the date is confirmed, although it is not reccommended.

Since you have already implemented a stop condition, just set stop to 1 after the date is confirmed and the while loop will not continue running.

Here's a better solution using a boolean:

bool stop = false;
while (!stop)
{
    Console.WriteLine("Please verify your birth date");
    userValueTwo = Console.ReadLine();

     try
     {
        DateTime birthdayTwo = DateTime.Parse(userValueTwo);
     }
     catch
     {
         Console.WriteLine("You did not enter a valid format.");
         Console.ReadLine();
     }

     if (userValueTwo == userValue)
     {
         Console.WriteLine("Birth date confirmed.");
         Console.ReadLine();
         stop = true;
     }

    else 
    {
        Console.WriteLine("Your birthday did not match our records. Please try again");
        Console.ReadLine();
    }
}
Sign up to request clarification or add additional context in comments.

3 Comments

Bingo! I was just going to suggest that someone throw a code example in their answer...
I am trying it like this and getting the error on the (!stop). "Use of unassigned local variable stop"
Ah, just set it so false. Editing answer to reflect it.
0

After it's confirmed, use break; in the line of code following.

Comments

0

You have entered an infinite loop. Infinite loops typically when a user has not fulfilled one or more requirements for the code to "hop" on back out of the loop To exit a loop once a objective is fulfilled, use "break;", to escape out of that current loop. In cases where a loop is nested, this might need to happen more than once

Comments

0

You never change the value of stop.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.