-2

i need c# code in which user can login with 3 attempts and user id should be case insensitive and password should be case sensitive would you please any one help. The code is given below:

C# code:

<code>
    //Login Attempts counter
    int loginAttempts = 0;

    //Simple iteration upto three times
    for (int i = 0; i < 3; i++)
    {
        Console.WriteLine("Enter username");
        string username = Console.ReadLine();
        Console.WriteLine("Enter password");
        string password = Console.ReadLine();

        if (username != "EDUAdmin" || password != "edu@123")
            loginAttempts++;
        else
            break;
    }

    //Display the result
    if (loginAttempts > 2)
        Console.WriteLine("Login failure");
    else
        Console.WriteLine("Login successful");

    Console.ReadKey();
</code>
  • What's wrong with your current code? The only obvious thing you should change is the if statement, you should check if username AND password match not one or the other. Therefore change || to &&. – Adrian Sep 03 '17 at 13:16
  • And you didn't try to look for `case insensitive string comparison in C#` on Google. https://msdn.microsoft.com/en-us/library/zkcaxw5y(v=vs.110).aspx – Chetan Sep 03 '17 at 13:20
  • What's the problem with your code ? If it's case insesnsitive for username, You may convert the user input and the real username to lower case using .ToLower() – Youssef13 Sep 03 '17 at 13:22
  • @Adriani6 i need the which can accept username as case insensitive and password should be case sensitive. if the inputs are wrong the it will show the message invalid loginid and password plzz try again – Mujtaba Hussain Bhat Sep 03 '17 at 13:23
  • @MujtabaHussainBhat Then do username.toLower() on username as well as change the username string to "eduadmin" and leave the password as it is, but still chagne to &&... – Adrian Sep 03 '17 at 13:25
  • Your question has nothing to do with the login attempt, you're just asking for case sensitive/insensitive string comparison which could be easily founded using any search engine. Here's a related question with answer: https://stackoverflow.com/questions/3121957/how-can-i-do-a-case-insensitive-string-comparison – T.Aoukar Sep 03 '17 at 13:25
  • @Adriani6 can you please update the code that will more appricited – Mujtaba Hussain Bhat Sep 03 '17 at 13:27
  • @ T.Aoukar actually that is first criteria and the case sensitive and case insensitive – Mujtaba Hussain Bhat Sep 03 '17 at 13:28
  • @MujtabaHussainBhat Posted as an answer. Not going to edit your question as it will no longer become a relevant part of this post. – Adrian Sep 03 '17 at 13:30
  • @Adriani6 actually i am new for c# thats why i asking for that can you please help me out – Mujtaba Hussain Bhat Sep 03 '17 at 13:32
  • @MujtabaHussainBhat do you want us to school you?? please learn basics of programming urself – harishr Sep 03 '17 at 13:37
  • if ((string.Compare(username, "EDUAdmin", true) == 0) && (string.Compare(password, "edu@123", false) == 0)) break; – Amit Kumar Singh Sep 03 '17 at 13:43

1 Answers1

0

Please see comments under Question for clarification.

//Login Attempts counter
int loginAttempts = 0;

//Simple iteration upto three times
for (int i = 0; i < 3; i++)
{
    Console.WriteLine("Enter username");
    string username = Console.ReadLine();
    Console.WriteLine("Enter password");
    string password = Console.ReadLine();

    if (username.ToLower() != "eduadmin" || password != "edu@123")
        loginAttempts++;
    else
        break;
}

//Display the result
if (loginAttempts > 2)
    Console.WriteLine("Login failure");
else
    Console.WriteLine("Login successful");

Console.ReadKey();
Adrian
  • 8,271
  • 2
  • 26
  • 43
  • @RomaDoskoch Oops, I didn't realise it, for some reason I thought it goes straight to login success. My bad, still too early for me. Apologies and fixed. – Adrian Sep 03 '17 at 13:33