Click here to Skip to main content
15,899,475 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication5
{
    class Program
    {
        public static string ReadPassword()
        {
            string password = "";
            ConsoleKeyInfo info = Console.ReadKey(true);
            while (info.Key != ConsoleKey.Enter)
            {
                if (info.Key != ConsoleKey.Backspace)
                {
                    Console.Write("*");
                    password += info.KeyChar;
                }
                else if (info.Key == ConsoleKey.Backspace)
                {
                    if (!string.IsNullOrEmpty(password))
                    {
                        // remove one character from the list of password characters
                        password = password.Substring(0, password.Length - 1);
                        // get the location of the cursor
                        int pos = Console.CursorLeft;
                        // move the cursor to the left by one character
                        Console.SetCursorPosition(pos - 1, Console.CursorTop);
                        // replace it with space
                        Console.Write(" ");
                        // move the cursor to the left by one character again
                        Console.SetCursorPosition(pos - 1, Console.CursorTop);
                    }
                }
                info = Console.ReadKey(true);
            }

            // add a new line because user pressed enter at the end of their password
            Console.WriteLine();
            return password;
        }
        static void Main(string[] args)
        {
            Random numberMaker = new Random();
            int number = numberMaker.Next(1, 100);
            Console.WriteLine("Please input your name below.");
            Console.Write("Name: ");
            string name = Console.ReadLine();
            if (name == "")
            {
                Console.WriteLine("\nERROR");
                name = "ERROR";
            }
            Console.WriteLine("\nI'm thinking of a number between 1 and 100.");
            int guess = 0;

                do
                {
                    Console.Write("Take a guess: ");
                    guess = int.Parse(Console.ReadLine());

                    if (number > guess)
                    {
                        Console.WriteLine("\nToo low");
                    }
                    else if (number < guess)
                    {
                        Console.WriteLine("\nToo high");
                    }
                    else if (name == "Logan")
                    {
                        string PASSWORD = "998783";
                        Console.Write("\nPassword: ");
                        string password = Console.ReadLine();

                            if (password == PASSWORD)
                            {
                                Console.WriteLine("\nAccess Granted.");
                                Console.WriteLine("\nWelcome {0}.\n", name);

                                int num1;

                                int num2;

                                string operand;

                                float answer;





                                Console.Write("\nPlease enter the first integer: ");

                                num1 = Convert.ToInt32(Console.ReadLine());





                                Console.Write("Please enter an operand (+, -, /, *): ");

                                operand = Console.ReadLine();





                                Console.Write("Please enter the second integer: \n");

                                num2 = Convert.ToInt32(Console.ReadLine());



                                switch (operand)
                                {

                                    case "-":

                                        answer = num1 - num2;

                                        break;

                                    case "+":

                                        answer = num1 + num2;

                                        break;

                                    case "/":

                                        answer = num1 / num2;

                                        break;

                                    case "*":

                                        answer = num1 * num2;

                                        break;

                                    default:

                                        answer = 0;

                                        break;

                                }



                                Console.WriteLine(num1.ToString() + " " + operand + " " + num2.ToString() + " = " + answer.ToString());

                            }
                            else
                            {
                                Console.WriteLine("\nERROR");
                                break;
                            }
                    }
                    else
                    {
                        Console.WriteLine("\nYou win!");
                    }
                } while (number != guess);
                
            Console.ReadLine();
            Console.WriteLine("Good bye, {0}.",name);
            Console.ReadLine();
        }
    }
}
Posted

What the... ?
You got the ReadPassword method from here: http://rajeshbailwal.blogspot.hu/2012/03/password-in-c-console-application.html[^]. You simply don't use it. Instead of
C#
string password = Console.ReadLine();

use
C#
string password = ReadPassword();


But pelase try at least to do your homework....
 
Share this answer
 
v2
Comments
Sergey Alexandrovich Kryukov 7-Dec-14 23:36pm    
5ed.
—SA
Ask Google, e.g. Password masking console application[^].
Cheers
Andi
 
Share this answer
 

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