Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

C# - while loop ignoring input?

  • 08-12-2011 05: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, Registered Users 2 Posts: 12,026 ✭✭✭✭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, Registered Users 2 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