I need help writing this simple C++ function.

Discussion in 'C++' started by jj_evans, Jun 8, 2010.

  1. jj_evans

    jj_evans New Member

    Joined:
    Jun 2, 2010
    Messages:
    5
    Likes Received:
    0
    Trophy Points:
    0
    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);
     
  2. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    Unless there is some logic (Which I cannot get from your post) just add Switch Case for each input
     
  3. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,009
    Likes Received:
    203
    Trophy Points:
    63
    Occupation:
    Senior Support Engineer
    Location:
    England
    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';
    }
    
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice