0

In my while loop it prompts the user to enter a value and once value is entered operates certain methods. The problem I am having is that the user is prompted to enter the value twice (i.e. asks to enter A, B or X. User enters value then program asks again to enter A,B or X without performing the correct operation). Any help to have it operate after being entered would be great!

         class output
         {
         static void Main(string[] args)
         {
            char userInput;
            char upper;

            Accounts myAccounts = new Accounts();
            myAccounts.input();
            Console.WriteLine("Enter an A to search account numbers");
            Console.WriteLine("Enter a B to average the accounts");
            Console.WriteLine("Enter X to quit the program");
            Console.WriteLine("Enter an option --->");

            userInput = Convert.ToChar(Console.ReadLine());
            upper = char.ToUpper(userInput);

            while (upper != 'X')
            {

                if (upper == 'A')
                {
                    myAccounts.search();
                    Console.WriteLine("Enter an A to search account numbers");
                    Console.WriteLine("Enter a B to average the accounts");
                    Console.WriteLine("Enter X to quit the program");
                    Console.WriteLine("Enter an option --->");
                    userInput = Convert.ToChar(Console.ReadLine());
                    upper = char.ToUpper(userInput);
                }
                else if (upper == 'B')
                {
                    myAccounts.average();
                    Console.WriteLine("Enter an A to search account numbers");
                    Console.WriteLine("Enter a B to average the accounts");
                    Console.WriteLine("Enter X to quit the program");
                    Console.WriteLine("Enter an option --->");
                    userInput = Convert.ToChar(Console.ReadLine());
                    upper = char.ToUpper(userInput);
                }
                else
                    Console.WriteLine("You entered an incorrect option, please select new option");
                Console.WriteLine("Enter an A to search account numbers");
                Console.WriteLine("Enter a B to average the accounts");
                Console.WriteLine("Enter X to quit the program");
                Console.WriteLine("Enter an option --->");
                userInput = Convert.ToChar(Console.ReadLine());
                upper = char.ToUpper(userInput);
            }        
        }
    }
2
  • Srsly.. read about methods... E.g. PrintMenu() EvalInput(char input) etc.. Commented Sep 14, 2014 at 20:30
  • What's in the average, search and input methods? Commented Sep 14, 2014 at 20:50

2 Answers 2

2

Try reformatting your main like this:

 static void Main(string[] args)
 {
    char userInput;
    char upper;

    Accounts myAccounts = new Accounts();
    myAccounts.input();

    do
    {

        Console.WriteLine("Enter an A to search account numbers");
        Console.WriteLine("Enter a B to average the accounts");
        Console.WriteLine("Enter X to quit the program");
        Console.WriteLine("Enter an option --->");
        userInput = Convert.ToChar(Console.ReadLine());
        upper = char.ToUpper(userInput);

        if (upper == 'A')
        {
            myAccounts.search();
        }
        else if (upper == 'B')
        {
            myAccounts.average();

        }
        else
            Console.WriteLine("You entered an incorrect option, please select new option");
    }   
    while (upper != 'X')     
}

Its not perfect, but is a good starting point. Basically why you are seeing your prompt twice is because you show the menu once before entering your while loop, then again after executing the operation. the do while loop I have provide should fix your problem.

Sign up to request clarification or add additional context in comments.

1 Comment

And also if a flow happens to execute, he is printing the menu and also when the flow exits. This question is really a time waster. Had he put braces in the else he'd have realized the problem. In fact the lack of braces on the else makes me suspect this to be a deliberate question (cynical).
0

An alternate method where menu display and user input is handled externally in a function

int iChoice;

while ((iChoice = ShowMenuAndReturnSelection()) != 0)
{
    switch (iChoice)
    {
        case 1:
            Console.WriteLine("You entered ONE.");
            break;
        case 2:
            Console.WriteLine("You entered TWO.");
            break;
        default:
            Console.WriteLine("Not a valid option.");
            break;
    }
}

int ShowMenuAndReturnSelection()
{
    Console.WriteLine("Menu (0 or invalid to exit)");
    Console.WriteLine("1. Executes Option 1");
    Console.WriteLine("2. Executes Option 2");
    Console.Write("Enter option: ");

    return Int32.TryParse(Console.ReadLine(), out int result) ? result: 0;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.