making a password typed appear as ***** in C#

Started by
10 comments, last by JonathanCCC 16 years, 3 months ago
Quote:Original post by SiCrane
If you want a drop in replacement for Console.ReadLine() you'd need something like:
    static string ReadLineAsPassword() {      ConsoleKey k;      string pass = "";      do {        ConsoleKeyInfo key_info = Console.ReadKey(true);        k = key_info.Key;        if (k != ConsoleKey.Enter) {          Console.Write('*');          pass += key_info.KeyChar;        }      } while (k != ConsoleKey.Enter);      Console.WriteLine();      return pass;    }


Ew.

static string ReadLineAsPassword() {  string password = ""; // as a Pythonista, calling it 'pass' messes with my head ;)  while (true) {    ConsoleKeyInfo key_info = Console.ReadKey(true);    if (key_info.Key == ConsoleKey.Enter) {      Console.WriteLine();      return password ;    }    Console.Write('*');    password += key_info.KeyChar;  }}


(EDIT: Actually, this isn't good enough; what about backspace?)
Advertisement
thanks alot for all of your help everyone.

I have used the last bit of code posted here and it seems to work ok. The only problem I have now is I do not know how to pass the result of password back to a variable in Main() so I can use it, before the value was in the "firstpick" variable, but now it's in the password variable in ReadLineAsPassword(). I need it to go into a variable called firstpick just after the "ReadLineAsPassword()" call in Main. Just incase you don't know what I mean, heres my source code:

using System;using System.Collections.Generic;using System.Text;namespace GuessNumber{    class Program    {        static string ReadLineAsPassword()        {            string password = "";            while (true)            {                ConsoleKeyInfo key_info = Console.ReadKey(true);                if (key_info.Key == ConsoleKey.Enter)                {                    Console.WriteLine();                    return password;                }                Console.Write('*');                password += key_info.KeyChar;            }        }        static void Main(string[] args)        {            Console.WriteLine("               -------------------------------");            Console.WriteLine("               |   WELCOME TO GUESS NUMBER   |");            Console.WriteLine("               -------------------------------");            Console.WriteLine();            Console.WriteLine();            Console.WriteLine();            Console.WriteLine("Please select game type:\n\nType S for singleplayer or M for multiplayer");            string sm = Console.ReadLine();            sm = sm.ToUpper();            while (sm != "S" && sm != "M")            {                Console.WriteLine();                Console.WriteLine("Invalid choice\n\nType S for singleplayer or M for multiplayer");                sm = Console.ReadLine();                sm = sm.ToUpper();            }            if (sm == "M")            {                MP();            }            if (sm == "S")            {                SP();            }        }        static void SP()        {            Console.WriteLine("TO DO SP");        }        static void MP()        {            Console.WriteLine();            Console.WriteLine();            Console.WriteLine();            Console.WriteLine("Multiplayer chosen");            Console.WriteLine();            Console.Write("Player 1 please enter your name: ");            string p1 = Console.ReadLine();            Console.WriteLine();            Console.Write("Player 2 please enter your name: ");            string p2 = Console.ReadLine();            Console.WriteLine();            Console.WriteLine();            Console.WriteLine("Game will consist of 4 rounds");            Console.WriteLine();            Console.WriteLine();            Console.WriteLine(p1 + " will guess first");            Console.WriteLine();            Console.Write(p2 + " please pick a number 1 to 10: ");            ReadLineAsPassword();            while (firstpick < 1 || firstpick > 10)            {                Console.WriteLine();                Console.WriteLine("Invalid choice\n\n");                Console.Write(p2 + " please pick a number 1 to 10: ");                ReadLineAsPassword();            }                Console.WriteLine();                Console.WriteLine(p2 + " has picked.");                Console.WriteLine();                Console.Write(p1 + " please enter a guess 1 to 10: ");                string firstguessstring = Console.ReadLine();                int firstguess = int.Parse(firstguessstring);                while (firstguess < 1 || firstguess > 10)                {                    Console.WriteLine();                    Console.WriteLine("Invalid choice\n\n");                    Console.Write(p1 + " please enter a guess 1 to 10: ");                    firstguessstring = Console.ReadLine();                    firstguess = int.Parse(firstguessstring);                }                Console.WriteLine();                Console.WriteLine();                Console.WriteLine("Results for round 1:");                Console.WriteLine();                Console.WriteLine(p2 + " picked " + firstpick);                Console.WriteLine();                Console.WriteLine(p1 + " guessed " + firstguess);                Console.WriteLine();                if (firstpick == firstguess)                {                    Console.WriteLine(p1 + " wins round 1!");                }                else                {                    Console.WriteLine(p2 + " wins round 1!");                }            }        }    }

This topic is closed to new replies.

Advertisement