-3

I'm trying to write some simple c# code that will validate a username and password, both the username and password are already in the code itself. Moreover, the validation is from a simple username and password - not from any sql database.

If the validation is correct I would like to print (output) - 'correct identification', if the username and/or password is wrong however I would like to output - 'wrong identification'.

My question (as posted by crashmstr) how do I check that the user entered the hard-coded username and password. Which leads to - the OP doesn't seem to know how to check that.

Why is my code outputting 'correct identification', regardless of the input?

            string username = "Pinocchio";
            string password = "Disney";

            Console.WriteLine("Enter Username: ");
            char answer = console.ReadLine()[0];

            Console.WriteLine("Enter Password: ");
            char answer2 = console.ReadLine()[1];

            if (!(username == "Pinocchio" && password == "Disney")) {
                Console.WriteLine("Correct Identification");

            }

            else
            {

                 Console.WriteLine("Wrong Identification");
            }
        }
    }
}

I have this which works... and I can always add a little bit more code later.

string password = "hello";
string username = "how";

if(Console.ReadLine() == password && Console.ReadLine() == username)
{
    Console.WriteLine("Correct Identification");
}
else 
{
    Console.WriteLine("Wrong Identification");
}
Green
  • 75
  • 2
  • 8
  • so what exactly is your question? and a hint: never store passwords plaintext and hardcoded – Matthias Burger Aug 29 '16 at 13:21
  • Possible duplicate of [Using if to compare strings c#](http://stackoverflow.com/questions/23025705/using-if-to-compare-strings-c-sharp) – Igor Aug 29 '16 at 13:22
  • 2
    You don't use `answer` or `answer2` in your `if`, and of course you will get the true path since you set those values yourself. – crashmstr Aug 29 '16 at 13:22
  • what is your question? – Hitesh Thakor Aug 29 '16 at 13:22
  • 7
    I'm curious why you're accepting a `char` for a username and password. – Drew Kennedy Aug 29 '16 at 13:25
  • This piece of code shows clear misunderstanding of programming. It's a series of `why` questions (why defining `username`, why using name `answer` instead, why taking first character, why `!` in condition) and required a patient teacher explanation of all such points. Ask a teacher. This question is not for SO and shouldn't be answered here. – Sinatr Aug 29 '16 at 13:35

1 Answers1

2

Let's break down your expression.

This:

(username == "Pinocchio" && password == "Disney")

yields true since both strings match.

Then you place a ! in front of it:

(!(username == "Pinocchio" && password == "Disney"))

That makes !true, which is false. Hence the username and password are deemed wrong.

Just removed the !:

(username == "Pinocchio" && password == "Disney")

I guess you need something like this:

Console.WriteLine("Enter User name: ");
string enteredUsername = console.ReadLine();

Console.WriteLine("Enter Password: ");
string enteredPassword = console.ReadLine();

if (username == enteredUsername && password == enteredPassword)
{ ... }
Patrick Hofman
  • 153,850
  • 22
  • 249
  • 325
  • Except the user input is `answer` and `answer2`... – crashmstr Aug 29 '16 at 13:23
  • 4
    There is no code performing any check on the input. The input is ignored. – Patrick Hofman Aug 29 '16 at 13:23
  • 1
    yes but the input-variables are not used and the username/password-variables are set hardcoded – Matthias Burger Aug 29 '16 at 13:23
  • 2
    I *think* the question is how to check that the user entered the hard-coded username and password, and the OP doesn't seem to know how to check that. (in other words: yes, the OP's code ignores the user input, but it should probably do something with it). – crashmstr Aug 29 '16 at 13:25