Beginner programming question?
-
What am I doing wrong in this snippet of code? I can't seem to figure out how to make this work: 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"); } } Any help would be greatly appreciated
-
What am I doing wrong in this snippet of code? I can't seem to figure out how to make this work: 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"); } } Any help would be greatly appreciated
First: you should get an compiler error with the consts - use
const string pass1 = "home";
for example Second: in your while-loop you are just using the Writeline (no block) - use this:**{** Consoloe.WriteLine(.... password = .... **}**
Then your problem is with the ORs (||): You iterate the while-loop as long the password is not pass1 OR not pass2 OR not pass3 and as pass1 != pass2 != pass3 you will satisfy this contition no matter what the input is - so just use && instead of || (substituting OR with AND) and it will work. -
First: you should get an compiler error with the consts - use
const string pass1 = "home";
for example Second: in your while-loop you are just using the Writeline (no block) - use this:**{** Consoloe.WriteLine(.... password = .... **}**
Then your problem is with the ORs (||): You iterate the while-loop as long the password is not pass1 OR not pass2 OR not pass3 and as pass1 != pass2 != pass3 you will satisfy this contition no matter what the input is - so just use && instead of || (substituting OR with AND) and it will work.