Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C++ (http://www.go4expert.com/forums/cpp/)
-   -   I need help writing this simple C++ function. (http://www.go4expert.com/forums/i-help-writing-simple-cpp-function-t22353/)

jj_evans 9Jun2010 02:26

I need help writing this simple C++ function.
 
Hi everyone, I need your help to write this C++ function. Every month corresponds to a number. I need the function to return the value for the month.

For example, user input month as MM (i.e. aug is 08)

jan returns 0, feb 3, mar returns 3 and so on.

how do I do this?

This is what I've got so far:

Function Prototype:

int monthValue(int month)
//returns a certain value for every month



Function Definition:

int monthValue(month, year)
{

}

What should I do in the function definition/body?


Here is the full list of values



int jan(0), , feb(3), mar(3), apr(6), may(1), jun(4), jul(6), aug(2), sep(5), oct(0), nov(3), dec(5);

shabbir 9Jun2010 10:21

Re: I need help writing this simple C++ function.
 
Unless there is some logic (Which I cannot get from your post) just add Switch Case for each input

xpi0t0s 9Jun2010 17:23

Re: I need help writing this simple C++ function.
 
There are lots of ways to do it. Here's one.
Code:

int monthValue(int month,int year)
{
  if (month==2 && is_leap_year(year))
  {
    // you didn't say what value to return here
    // drop this block if leap years are handled elsewhere
    // but the fact that year is passed in suggests they're not
  }
  else return month["0033614625035"]-'0';
}



All times are GMT +5.5. The time now is 15:34.