Click here to Skip to main content
15,891,864 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
using System.Threading;
using System;


class Sample
{
    public static void Main()
    {
        string user;
        string password = "";
        string cor = "i";
        string pass = "1";
        do
        {
            Console.Write("Username: ");
            user = Convert.ToString(Console.ReadLine());

        } while (user != cor);
        {
            Console.Write("Enter your password: ");
            ConsoleKeyInfo key;
            do
            {
                key = Console.ReadKey(true);
                if (key.Key != ConsoleKey.Backspace)
                {
                    password += key.KeyChar;
                    Console.Write("*");
                }
                else
                {
                    if (password.Length > 0)
                    {
                        password = password.Substring(0, (password.Length - 1));
                        Console.Write("\b \b");
                    }
                }
            }
            while (key.Key != ConsoleKey.Enter);
            


            Console.WriteLine();
            Console.WriteLine("The Password You entered is : " + password);

            {
                if (password != pass)
                {
                    Console.WriteLine();
                    Console.WriteLine("PASSWORD DECLINED");
                    //Thread.Sleep(700);

                }
                else if (password == pass)
                {
                    Console.WriteLine();
                    Console.WriteLine("PASSWORD ACCEPTED");
                    //Thread.Sleep(700);
                }
                else Console.WriteLine("ERROR"); 
                Console.ReadKey();
            }
        }
    }
}

I'm using .NET framework 3.5
I'm super new to programming and I am unsure why this is not working.
Any tips or corrections would be helpful.
Posted
Comments
Marc A. Brown 11-May-11 12:28pm    
What are you expecting the code to do and what is it doing wrong?

The problem is quite simple: You add every character to your password, including the ENTER key at the end.
So, when you do the password check, you compare "i" with "i\r" which will never match.

Two ways round this:
1) Add the '\r' character to your password. Nasty, brute-force-and-ignorance approach.
2) Re-work your loop:
ConsoleKeyInfo key = Console.ReadKey(true);
while (key.Key != ConsoleKey.Enter)
    {
    if (key.Key != ConsoleKey.Backspace)
        {
        password += key.KeyChar;
        Console.Write("*");
        }
    else
        {
        if (password.Length > 0)
            {
            password = password.Substring(0, (password.Length - 1));
            Console.Write("\b \b");
            }
        }
    key = Console.ReadKey(true);
    }

You could have found this yourself, very easily. Since the problem is that the two strings don't appear to match, when you think they should, you need to find out why not.
So. put a breakpoint on the line:
if (password != pass)
The execution of your program will then stop there, and you can look at (and change if you need to) the values "password" and "pass". You can then start looking back in your code to find the source of the problem.


But while you are here:
if (password != pass)
    {
    ...
    }
else if (password == pass)
    {
    ...
    }
else
    ...
Doesn't need to be that complex. Since password either is or isn't equal to pass, it is normal to just write:
if (password != pass)
    {
    ...
    }
else
    {
    ...
    }
(Unless you are feeling really paranoid and suspect that a bool has three states: "true", "false" and "quantum". It doesn't, in .NET. Honest.)
 
Share this answer
 
//comment your else block and add one more condition in if block then it will work fine
//if (key.Key != ConsoleKey.Backspace && key.Key != ConsoleKey.Enter )

using System.Threading;
using System;

class Sample
{
public static void Main()
{
string user;
string password = "";
string cor = "i";
string pass = "1";
do
{
Console.Write("Username: ");
user = Convert.ToString(Console.ReadLine());
} while (user != cor);
{
Console.Write("Enter your password: ");
ConsoleKeyInfo key;
do
{
key = Console.ReadKey(true);
if (key.Key != ConsoleKey.Backspace && key.Key != ConsoleKey.Enter )
{
password += key.KeyChar;
Console.Write("*");
}
/*else
{
//if (password.Length > 0)
{
// password = password.Substring(0, (password.Length - 1));
//Console.Write("\b \b");
}
}*/
}
while (key.Key != ConsoleKey.Enter);

Console.WriteLine();
Console.WriteLine("The Password You entered is : " + password);
{
if (password != pass)
{
Console.WriteLine();
Console.WriteLine("PASSWORD DECLINED");
//Thread.Sleep(700);
}
else if (password == pass)
{
Console.WriteLine();
Console.WriteLine("PASSWORD ACCEPTED");
//Thread.Sleep(700);
}
else Console.WriteLine("ERROR");
Console.ReadKey();
}
}
}
}
 
Share this answer
 
v2
Comments
Christian Graus 3-Jul-11 22:14pm    
This was answered weeks ago, why are you guys posting to something that's been answered ?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900