0

What is wrong with my do while loop logic?

I want to prevent users from proceeding further into the application unless they provide the correct input.

Currently, it will only accept one wrong input before it crashes and produces this exception:

System.Exception: at disabler.Program.readInput(String Input) at disabler.Program.detectInput(String Input) at disabler.Program.Main(String[] args)

Here is my code:

   private static string detectInput(string Input )
    {
        string result= Input;
        do
        {
            //pass input to readInput method
            if ((result == "exit") || (result == "query") || (result == "disable"))
            {
                readInput(result);
            }
            else 
            {
                Console.WriteLine("invalid input detected. Please try again.");
                result= Console.ReadLine();
                readInput(result);
            }
        }
        while (!((result == "exit") || (result == "query") || (result == "disable")));
        return result;
    }

Here is the code for readInput method:

//Read user input
    private static string readInput(string Input)
    {
        int successfullyDisabled = 0;
        string input = string.Empty;
        switch (Input)
        {
            case "disable":
                            int disableCounter = 0;
                invalidAccount.ForEach(delegate(String samAccountName)
                {
                    disableCounter++;
                    Console.Write(disableCounter);
                    //disable inactive accounts
                    if (DisableADUser(samAccountName))
                    {
                        successfullyDisabled++;
                    }
                });

                invalidAccount.Clear();                    
                validAccount.Clear();
                Console.WriteLine("Press [ENTER] to continue or [ESC] to exit.\n");
                break;
            case "query":                   
                            Console.WriteLine("No.\t  | Dep | User Account\t       | Status");                               
                            int countRow = 0;
                validAccount.ForEach(delegate(String samAccountName)
                {
                    countRow++;
                    Console.Write(countRow);
                    //find status of active accounts
                    findUserStatus(samAccountName);
                });
                validAccount.Clear();
                invalidAccount.Clear();
                Console.WriteLine("Press [ENTER] to continue or [ESC] to exit.\n");
                break;
            case "exit":
                //leave console
                Environment.Exit(2);
                invalidAccount.Clear();
                validAccount.Clear();
                break;
            default:
                throw new Exception("Invalid command entered. Please enter command again.");
        }
        return input;
    }//end of ReadInput
iNeedHelp
  • 225
  • 1
  • 2
  • 10
  • 1
    What is the exception message? What does `readInput` do? – Mike Norgate Apr 17 '15 at 06:29
  • 2
    The exception shows that it is being thrown by the `readInput` method. In order to assist anyone who might be interested in answering this question, would you be so kind as to include it's code. – Alex Apr 17 '15 at 06:29
  • 4
    Did you _really_ want to check for `exit` twice? – paxdiablo Apr 17 '15 at 06:30
  • Why are you checking for `exit` twice in the same expression? (It would also *really* help readability if you'd follow normal .NET naming conventions.) Please provide a short but *complete* program demonstrating the problem. – Jon Skeet Apr 17 '15 at 06:31
  • Oh, didn't notice that typo. I've updated my code – iNeedHelp Apr 17 '15 at 06:38
  • In your new edit, I see you are throwing an exception in readInput, but not catching it in the calling function while still having logic that implies you expect the code to continue to process. – Scott 'scm6079' Apr 17 '15 at 06:38
  • 2
    You would be better off setting a break point and stepping through your methods, it doesn't seem very friendly to me that if you enter something invalid in `readInput` you throw an exception. – Sayse Apr 17 '15 at 06:38
  • 1
    sorry but the best answer I could provide is *use the debugger* - really no offense but you would save yourself (and us) a lot of time if you`d learn to use this invaluable tool – Random Dev Apr 17 '15 at 06:39

1 Answers1

3

The exception you are getting is the exception that you are throwing in the switch statement of readInput

Remove readInput(result); from the else of detectInput

Update based on comments:

private static string detectInput(string Input )
{
    string result= Input;
    while (!((result == "exit") || (result == "query") || (result == "disable")));
    {            
            Console.WriteLine("invalid input detected. Please try again.");
            result= Console.ReadLine();
        }
    }

    result = readInput(result);

    return result;
}
Mike Norgate
  • 2,393
  • 3
  • 24
  • 45
  • please edit your answer and show the corrected code and I will happily upvote it – Random Dev Apr 17 '15 at 06:41
  • You may want to expand on this answer a bit, because it is correct, but it is not clear from how you written it, and requires a bit more elaboration. – Alex Apr 17 '15 at 06:41