Im creating a little practice commend line application that has to ask for passwords.This is a section of the code that is suppose to run if the user puts spaces in a password.

It works fine up to that point but then no matter what the user inputs it always passes to the else stament and exits.
Heres that part of the code:

  if (hasSpace == true) //Checks for spaces and asks to rewrite if true
                  {
                      Console.WriteLine("Your password may not contain spaces, would you like to try again (Y,N)");
                      ConsoleKeyInfo input = Console.ReadKey();
                      if (input.Key == ConsoleKey.Y)//Dosnt work
                      {
                          runagain = true;
                      }

                      if (input.Key == ConsoleKey.N)//Dosnt Work
                      {
                          Environment.Exit(0);
                      }
                      else
                      {
                          Environment.Exit(0);
                      }

                  }

I know theres probbably a very simple solution to this but nothing ive tried as worked so far.

You may intend for it to be in a loop and return to the top? I use a 'while (true)' loop as a control structure sometimes, with 'break;' at appropriate places to kick it back to the top or out of the loop.

You may intend for it to be in a loop and return to the top? I use a 'while (true)' loop as a control structure sometimes, with 'break;' at appropriate places to kick it back to the top or out of the loop.

Yes I am using runagain to allow the user to flip back to the top of the loop so he could try inputing it again, the problem isnt here though its somewhere in the if statements, no matter what the user inputs it always go to the else stament and exits.

I dont know if the problem could lie somewhere else in the code as its mostly not revelant to this snippet and is pretty straightfoward.

if (input.Key == ConsoleKey.Y)//Dosnt work { runagain = true;

Shouldn't you have a 'break;' after this (above)? Or make the next 'if' into an 'else if'?

Alright thanks, changing the second if statment to an else if worked.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.