9 Replies - 647 Views - Last Post: 15 February 2011 - 03:10 PM Rate Topic: -----

#1 Asji  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 0
  • View blog
  • Posts: 32
  • Joined: 11-January 11

!= or not != hmmm..

Posted 14 February 2011 - 10:48 PM

            // Program asks user to enter password
            // If password is not "home", "lady" or "mouse"
            // the user must re-enter the password
            //Chapter 4 Debug 1

            const string PASS1 = "home";
            const string PASS2 = "lady";
            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");

            Console.ReadKey();



so there is my code, yet another debugging exercise
my problem arrives here
while(password != PASS1 || password != PASS2 || password != PASS3)


I'm not sure what type of bool goes here??
when I replace the != with ==, anything BUT the 3 accepted values returns a "Valid password"

and I'm not totally sure how to fix this error, I got all confused when I went here
http://msdn.microsof...=VS.100%29.aspx
hope that returns the right link/page and not just the default msdn page

thanks in advance for any help
/cheers

This post has been edited by Asji: 14 February 2011 - 10:53 PM


Is This A Good Question/Topic? 0
  • +

Replies To: != or not != hmmm..

#2 Curtis Rutland  Icon User is offline

  • (╯°□°)╯︵ (~ .o.)~
  • member icon


Reputation: 3831
  • View blog
  • Posts: 6,475
  • Joined: 08-June 10

Re: != or not != hmmm..

Posted 14 February 2011 - 11:05 PM

Ok, first of all, what does "I'm not sure what type of bool goes here??" mean? Bool is a type. There's only one "type" of bool...and that's "bool".

Work the truth tables.

while(password != PASS1 || password != PASS2 || password != PASS3)


As your code is, the boolean statement will always result in true, because you're using ORs. If any one of those individual statements results in true, the entire statement is true, and since the password can't match all three, at least two will always be true. When you enter "home", the first one matches, means it is false. But the second one doesn't match, which is true, which means that the entire statement becomes true.

while (password == PASS1 || password == PASS2 || password == PASS3)


If you change them to "==", you have the opposite problem. The WHILE loop only executes when the statement equates to true, which means the stuff on the inside only runs when it's true. It will only be true when one or more of those statements results in true, which will only happen when you provide the right password.

Basically, you need to negate that entire statement. You need it to result in FALSE when it would be TRUE. To do so, use the Negation operator.

This post has been edited by insertAlias: 14 February 2011 - 11:06 PM

Was This Post Helpful? 2
  • +
  • -

#3 maffelu  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 36
  • View blog
  • Posts: 185
  • Joined: 21-August 08

Re: != or not != hmmm..

Posted 14 February 2011 - 11:16 PM

I think you want to do this:

while(password != PASS1 && password != PASS2 && password != PASS3)


Since it can't be ANY of those you want to read

while(passwords is not PASS1 AND password is not PASS2 AND password is not PASS3)
Was This Post Helpful? 1
  • +
  • -

#4 Asji  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 0
  • View blog
  • Posts: 32
  • Joined: 11-January 11

Re: != or not != hmmm..

Posted 14 February 2011 - 11:18 PM

Sorry about the initial confusion there
while(password != PASS1 || password != PASS2 || password != PASS3)

so what these != along with the OR ||, when the password is a correct value, it returns a false statement, but while the other 2 remain true
the OR || make the whole statement true?
Was This Post Helpful? 0
  • +
  • -

#5 Curtis Rutland  Icon User is offline

  • (╯°□°)╯︵ (~ .o.)~
  • member icon


Reputation: 3831
  • View blog
  • Posts: 6,475
  • Joined: 08-June 10

Re: != or not != hmmm..

Posted 14 February 2011 - 11:23 PM

http://en.wikipedia....i/Boolean_logic

You need a good read on what bools actually are. When you use OR, if either side is true, the entire statement is true. When you use AND, both must be true. When there are no parenthesis, the equation is resolved from left to right. So, if I have:

A || B || C


First I evaluate A || B. Let's assume that evaluates to FALSE. Now the statement is :

FALSE || C


See how that works?

@maffelu, the idea here isn't to give out answers to homework. That doesn't impart any learning, just an answer. Please avoid just handing out solutions.

This post has been edited by insertAlias: 14 February 2011 - 11:24 PM

Was This Post Helpful? 3
  • +
  • -

#6 Asji  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 0
  • View blog
  • Posts: 32
  • Joined: 11-January 11

Re: != or not != hmmm..

Posted 14 February 2011 - 11:31 PM

Yes it makes a lot more sense now!
Thanks a ton again :)
Sorry if I do ask some really easy questions, my professor don't really break it down to English very well.
We've gone through about 10 Chapters total in 2 books, and its really overwhelming for me
Was This Post Helpful? 0
  • +
  • -

#7 Jeff H  Icon User is online

  • D.I.C Regular

Reputation: 112
  • View blog
  • Posts: 305
  • Joined: 30-January 11

Re: != or not != hmmm..

Posted 15 February 2011 - 12:07 AM

With what you learned so far can figure out what to replace ??? with to make it work?
 while(???(password == PASS1 || password == PASS2 || password == PASS3))  


Was This Post Helpful? 0
  • +
  • -

#8 Asji  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 0
  • View blog
  • Posts: 32
  • Joined: 11-January 11

Re: != or not != hmmm..

Posted 15 February 2011 - 09:24 AM

I believe,
while(???(password == PASS1 || password == PASS2 || password == PASS3))

would be replaced with
while(!(password == PASS1 || password == PASS2 || password == PASS3))

as the ! would negate the value in quite an odd way, and I think it would work
Was This Post Helpful? 0
  • +
  • -

#9 Curtis Rutland  Icon User is offline

  • (╯°□°)╯︵ (~ .o.)~
  • member icon


Reputation: 3831
  • View blog
  • Posts: 6,475
  • Joined: 08-June 10

Re: != or not != hmmm..

Posted 15 February 2011 - 09:44 AM

Quote

! would negate the value in quite an odd way


Nothing odd at all. ! is the negation operator. It takes true and makes it false, and it takes false and makes it true.

Boolean logic is just math. Parenthesis still have the highest order of operations, so the statement inside will be evaluated first.

We know from working the logic that the statement inside returns the exact opposite of what we want. So, we negate it with !.

This post has been edited by insertAlias: 15 February 2011 - 09:45 AM

Was This Post Helpful? 1
  • +
  • -

#10 Asji  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 0
  • View blog
  • Posts: 32
  • Joined: 11-January 11

Re: != or not != hmmm..

Posted 15 February 2011 - 03:10 PM

thanks for nice explanation of how to use ! and !=
@maffelu,
Thanks for the ANSWER to the problem, but that's not quite what I was after, yes it did fix my problem, but with no explanation as to why it was a problem in the first place, nor a way to trouble-shoot a future problem of the same error with far more complexity.

Another thanks for this site and all the wonderful people that "troll" the forums to help "infants" like me :)
someday I hope to return the favor.
Thanks a ton!
/cheers
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1