Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

C# - while loop ignoring input?

Options
  • 08-12-2011 5:36pm
    #1
    Closed Accounts Posts: 4,763 ✭✭✭


    Hello!

    I'm doing a school exercise. I actually coded, finished and signed off on the program, before I decided to go back to it out of boredom. In v2.0 I've finding that the program is ignoring all input inside of a while loop, except the input that breaks the loop. I've been beating my head on the wall against this since yesterday. I thought at first that it was an issue with Monodevelop, but I took the code over to Visual Studio 2010 and found the problem replicating.

    The idea is to list all of the records in an array. I've tested the foreach loop separate to the while{X}, and it runs perfectly.

    A friend gave it a once-over last night, but found nothing awry:
            while (true) {
    
                // Clear the console to begin.
                Console.Clear();
    
                // The user can view records or exit, at this point.
                Console.WriteLine("Menu:");
                Console.WriteLine();
                Console.WriteLine("1. List records.");
                Console.WriteLine("2. View record.");
                Console.WriteLine("9. Exit.");
                Console.WriteLine();
    
                Console.Write("Enter Choice: ");
                int userMenuChoice = Convert.ToInt32(Console.ReadLine());
    
                if (userMenuChoice == 1)
                {
                    foreach (string name in employeeName)
                    {
                        index++;
                        Console.WriteLine("{0}. {1}", index, name);
                    }
                }
                else if (userMenuChoice == 9)
                {
                    break;
                }
    
            }
    


Comments

  • Registered Users Posts: 11,979 ✭✭✭✭Giblet


    It seems to be, that 1, would output some console stuff then clear immediately when it relooped (so quick, you couldn't see it). Try a console.readline or break after the writeline for the employeename


  • Closed Accounts Posts: 4,763 ✭✭✭Fenster


    Giblet wrote: »
    It seems to be, that 1, would output some console stuff then clear immediately when it relooped (so quick, you couldn't see it). Try a console.readline or break after the writeline for the employeename

    Christ. It really was that: The program was going so fast that it didn't show. After I finish standing in the corner with my head hung low in shame, I will raise a drink in your honour.


  • Registered Users Posts: 586 ✭✭✭Aswerty


    This would have been a problem that the debugger in VS (or any other decent IDE) could easily have found. You should get used to using breakpoints to allow you to pause the code and view how your code is behaving.


Advertisement