Though I would be able to figure this out but for some reason my if statements are just not working. What I'm trying to do is date validation. I call this function passing in day, month, year. The issue I'm having the the if statement. I can't seem to get the symbol / && / || in the order to return what I want. Which is if the month is 1 and day falls between 1-31 return true if not false.

bool isDateValid(int& d, int& m, int& y)
   if(m==1 || m==3 || m==5 || m==7 || m==8 || m==10 || m==12 && d<1 || d>31)
     {return false;}

    else if( m==4 || m==6 || m==9 || m==11 && d<1 || d>30)
     {return false;}

    else{return true;}

I know I left of Feb but I need to do other stuff for that month.

Edited by happikoto: n/a

5 Years
Discussion Span
Last Post by WaltP

The "&&" operator has higher precedence than the "||" operator. Your statement in line 3 is evaluating

m==12 && d<1

first, and then running the "||" evaluations.

Can you see how this isn't what you wanted?


Make 2 IF statements so it's more readable. If month { if day } Or put the number of days in an array and a simple

if (d <= MonthDays[m])

makes the job painless and readable.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.