Home > Software engineering >  having this issue where the code does not recognize whether or not the password inputted is correct
having this issue where the code does not recognize whether or not the password inputted is correct

Time:11-13

I have an issue making the program understand if the answer inputted by the user is correct or not. I tried using String.Empty instead but that did nit work im super confused right now as to why this isnt working, i did also try userInput = password and vice versa but that made it so the answer was always correct i honestly couldn't think of anything else this is how my code looks now:

       string password = "abcd1234";
        string userInput = String.Empty;
        string Text1 = "Please Insert Your Password";
        string Text2 = "Incorrect Password!";
        string Text3 = "Correct You May Proceed!";

         
        Console.WriteLine(Text1);
        Console.ReadLine();
        if (userInput == password)
            Console.WriteLine(Text3);            
        
        
        if (userInput != password)
            Console.WriteLine(Text2);

CodePudding user response:

please use this:

        string password = "abcd1234";
        string Text1 = "Please Insert Your Password";
        string Text2 = "Incorrect Password!";
        string Text3 = "Correct You May Proceed!";
        string userInput = Console.ReadLine();
        Console.WriteLine(Text1);
        if (password.Equals(userInput))
            Console.WriteLine(Text3);
        else 
            Console.WriteLine(Text2);

CodePudding user response:

Can I assume you have to hit enter before seeing your Text1 prompt, too?

string userInput = Console.ReadLine(); is listening for your input before anything else you see in your app. This line should be moved to after you prompt for your password.

The other ReadLine should be removed:

    Console.ReadLine(); \\this doesn't assign your input to anything

This line is waiting for input but doesn't do anything with it.

You do want the '==', by the way - this returns true if the two are equal. Single '=' is used for assignment, like you have earlier in this code.

  •  Tags:  
  • c#
  • Related