3 Replies - 467 Views - Last Post: 06 September 2012 - 09:20 AM Rate Topic: -----

#1 blitzgash  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 04-September 12

Debug Assignment.

Posted 04 September 2012 - 04:25 PM

Relatively new at C#.. I can't seem to figure out why this code is stuck in a while loop. Everything I try just keeps it in the loop?

Yes this is homework, and I'm stumped.. been at it for an hour..

using System;
public class DebugFive1
{
    public static void Main()
  {
   const string PASS1 = "home";
   const string PASS2 = "lady";
   const string PASS3 = "mouse";
   string password = "";
   

   Console.WriteLine("Please enter your password: ");
   password = Console.ReadLine();

   while (password.Equals(PASS1) == false || password.Equals(PASS2) == false || password.Equals(PASS3) == false)
   {
      Console.WriteLine("Invalid password. Please enter again: ");
      password = Console.ReadLine();
   }
   Console.WriteLine("Valid password");

   }
}



Is This A Good Question/Topic? 0
  • +

Replies To: Debug Assignment.

#2 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4421
  • View blog
  • Posts: 12,286
  • Joined: 18-April 07

Re: Debug Assignment.

Posted 04 September 2012 - 04:36 PM

Lets assume that they enter the password "lady". Would password.Equals(PASS1) be false? Yes, so the whole thing is true and the loop continues. I think you meant to use && not ||

With the "or" in between if any of those are true, the whole thing is true.

:)

P.S. You can compare strings like normal in C#... this isn't Java where you need to use the equals method to compare string values.

This post has been edited by Martyr2: 04 September 2012 - 04:40 PM

Was This Post Helpful? 2
  • +
  • -

#3 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2153
  • View blog
  • Posts: 3,311
  • Joined: 21-June 11

Re: Debug Assignment.

Posted 04 September 2012 - 04:50 PM

Your while condition consists of three conditions that are or-ed together. However no matter what password is, at least two of those conditions will always be true.

For example if password is "home", password.Equals(PASS1) == false will be false, but password.Equals(PASS2) == false and password.Equals(PASS3) == false will be true. So the whole condition will be true. Since this will be the case for all values of password, the condition will always be true and the loop will run forever.
Was This Post Helpful? 1
  • +
  • -

#4 Robin19  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 272
  • View blog
  • Posts: 552
  • Joined: 07-July 10

Re: Debug Assignment.

Posted 06 September 2012 - 09:20 AM

I have a tip with these types of problems. I found it reading the book Clean Code. Don't have a chain of && ans ||. Have a method. It improves readability and makes it easier to debug.

   static string PASS1 = "home";
   static string PASS2 = "lady";
   static string PASS3 = "mouse";

   public static void Main()
   {
   ...
   Console.WriteLine("Please enter your password: ");
   password = Console.ReadLine();

   while (!IsValidPassword(password))
   {
      ...
   }
   ...
   }
   
   static bool IsValidPassword(string EnteredPasscode)
   {
      // you could put your one line test here, but I like to break it out.
      if (EnteredPasscode == PASS1)
         return true;
      if (EnteredPasscode == PASS2)
         return true;
      if (EnteredPasscode == PASS3)
         return true;
      return false;
   }



Now it reads better without reading through a large block of || or &&. It also allows flexibility. IsValidPassword can be changed without breaking anything else. What happens if you add 20 more passwords? What happens if you keep all the passwords in a collection? What happens if you don't care about case in the password? Now all of these questions can be answered in a simple method and in a location anyone can find. You can also set a breakpoint at the method to see why it doesn't return the correct response.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1