Answer to challenging question

Posted: February 9, 2012 in OOP344

Here is my one line statement, which will work for this program…

int validMonth(int mon, char* errmes){
  return (strcpy(errmes, "Invalid month (1<=month<=12)") && mon > 0 && mon <=12);
}

NOTE:
Since someone has posted already the more efficient answer,

int validMonth(int mon, char* errmes){
  return (mon > 0 && mon <=12) || !strcpy(errmes, "Invalid month (1<=month<=12)");
}

I am just to show you here, this can be done with && operator. Also, my return statement will modify/replace  the errmes even if it is true. For good programming practice, we have to avoid extra process. So please don’t use this type of method.

Thanks!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s