Im am having abit of trouble with a while loop.

I have this method which determines wether or not the entered year is a leap year, the while loop is intended to keep asking the user for another year until he/shes wishes to exit the program.

Here is my method:

static int Main(string[] args)
        {

            InputWrapper iw = new InputWrapper();
            Console.WriteLine("Enter -1 at any time to terminate the program");
            int year = iw.getInt("Year: ");

            while(year != -1)
            {

                if(DateTime.IsLeapYear(year))
                {
                    Console.WriteLine("{0} is a leap year", year);                   
                }
                else
                {
                    Console.WriteLine("{0} is not a leap year", year);                  
                }               
                Console.ReadLine();
            }
            return 0;

        }

I have been trying to figure this out for a good hour, it seems basic enough but i cant get it.
How do I get it to keep repeating until the user exits?

I have no idea what InputWrapper is, but I change the code little bit to update the year value inside the loop which you have missed in your code

Console.WriteLine("Enter -1 at any time to terminate the program");
string read = Console.ReadLine();
int year = Convert.ToInt32(read);
while (year != -1)
{
    if (DateTime.IsLeapYear(year))
    {
        Console.WriteLine("{0} is a leap year", year);
    }
    else
    {
        Console.WriteLine("{0} is not a leap year", year);
    }
    read = Console.ReadLine();
    year = Convert.ToInt32(read);
}

Edited 2 Years Ago by niranga: Spelling mistake

Like ChrisHunter said, you could always use

while(true)
{
    break;
}

Okay so i added that break; just to remind you that when using a while true, that's the best way to escape the loop (break out of it). For instance, exiting the program

(On a personal note, be careful when using while(true) to me it doesn't matter how many years of coding experience I have, I always worry I'll get caught in an infinite loop with these, so I take extra caution when working with them)

This article has been dead for over six months. Start a new discussion instead.