Leap Years using while
1 2 3 4 5 6
|
while ((dd == 29 && mm == 2) && ((yr%4 = 0 && yr%100 !=0) || yr%400 == 0))
{
cout << "Invalid day" << endl
<< "Reenter the day: " << endl;
cin >> dd;
|
Had check around this forum & using google as well.
Seem there no example on using while statement for leap years.
Is it possible to use while statement for leap years?
Well, of course... you just confused = with ==.
Hi Athar,
1 2 3 4 5 6 7
|
while ((dd == 29 && mm == 2) && ((yr%4 == 0 && yr%100 !=0) || yr%400 == 0))
{
cout << "Invalid day" << endl
<< "Reenter the day: " << endl;
cin >> dd;
}
|
Year 2000 is a leap year but 1800 is not. If using above code, it will treat 1800 as leap year 2000 as non-leap year.
In order to make this sane, you should put that complicated mess of comparisons in a separate function:
1 2 3 4 5 6 7 8 9 10 11 12
|
bool IsDateValid(int m,int d,int y)
{
// return true if valid date
// return false otherwise
}
//...
while( !IsDateValid(mm,dd,yr) )
{
//...
}
|
Topic archived. No new replies allowed.