// Program asks user to enter password // If password is not "home", "house" or "mouse" // the user must re-enter the password using System; public class DebugFive2 { public static void Main() { const string pass1 = home; const string pass2 = house; const string pass3 = mouse; string password; Console.Write("Please enter your password "); password = Console.ReadLine(); while(password != pass1 || password != pass2 || password != pass3) Console.WriteLine("Invalid password. Please enter again. "); password = Console.ReadLine(); Console.WriteLine("Valid password"); } }
help with this code
Page 1 of 15 Replies - 931 Views - Last Post: 29 January 2010 - 12:04 AM
#1
help with this code
Posted 28 January 2010 - 05:26 PM
Replies To: help with this code
#2
Re: help with this code
Posted 28 January 2010 - 05:35 PM
What errors are you getting?
When you create your project you should choose Console Application since its gonna be on console so you get the appropiate template.
Well another error is that when difining the string variable it should be done like
notice the quotes
Also you are missing an openning bracket for the while and i think your while loop is infinite 
you should do a break if the pass is valid or something like that
EDIT: the condition on the while should be && not ||
When you create your project you should choose Console Application since its gonna be on console so you get the appropiate template.
Well another error is that when difining the string variable it should be done like
const string pass1 = "home"
notice the quotes

Also you are missing an openning bracket for the while

EDIT: the condition on the while should be && not ||
This post has been edited by poncho4all: 28 January 2010 - 05:40 PM
#4
Re: help with this code
Posted 28 January 2010 - 06:16 PM
If you are only looking for a way how to improve your code, this might give you an idea:
static void Main(string[] args) { string[] passwords = { "home", "house", "mouse" }; string entered_password = string.Empty; while (!passwords.Contains(entered_password)) { Console.Write("Please enter your password "); entered_password = Console.ReadLine(); } Console.WriteLine("Valid password"); }
This post has been edited by FlashM: 28 January 2010 - 06:17 PM
#5
Re: help with this code
Posted 28 January 2010 - 09:57 PM
FlashM, on 28 Jan, 2010 - 05:16 PM, said:
If you are only looking for a way how to improve your code, this might give you an idea:
static void Main(string[] args) { string[] passwords = { "home", "house", "mouse" }; string entered_password = string.Empty; while (!passwords.Contains(entered_password)) { Console.Write("Please enter your password "); entered_password = Console.ReadLine(); } Console.WriteLine("Valid password"); }
Cool idea is there a way once password entered it open a program for an example internet explore?
#6
Re: help with this code
Posted 29 January 2010 - 12:04 AM
System.Diagnostics.Process.Start("iexplore.exe");
Something like that I think :-)
Something like that I think :-)
Page 1 of 1