function to find out for many days have passed since last birthday.need help.

This is a discussion on function to find out for many days have passed since last birthday.need help. within the C++ Programming forums, part of the General Programming Boards category; Hello, I have to write a c++ program with my own function which consists of two parametrs (day, month). Function ...

  1. #1
    Registered User Solarwin's Avatar
    Join Date
    Dec 2012
    Posts
    50

    function to find out for many days have passed since last birthday.need help.

    Hello,
    I have to write a c++ program with my own function which consists of two parametrs (day, month). Function have to return number of days since the begining of this year. Using this function i have to find out how many days are left till birthday (or how many days have passed since last birthday)

    This is how far i am:
    Code:
    #include <iostream>
    
    using namespace std;
    
    
    int cikDienu(int diena, int menesis);
    int main()
    {
    
    
        int dd = 0;
        int mm = 0;
        int birthday = 0;
        int birthmonth = 0;
        int fromYearBegining = 0;
    
    
        cout << "Enter todays date: "<< endl;
        cout << "day: " << endl;
        cin >> dd;
        cout << "month: " << endl;
        cin >> mm;
        cout << "Enter your birthday: "<< endl;
        cout << "day: " << endl;
        cin >> birthday;
        cout << "month: " << endl;
        cin >> birthmonth;
        cout << "From this year begining have passed " << cikDienu(dd) << " days" << endl;
        return 0;
    }
    
    
    
    
    int cikDienu(int diena, int menesis){
        int fromyear;
        int howmuch;
        howmuch=fromyear - 365;
    
    
    
    
        return howmuch;
    }

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    2,778
    First your function call doesn't match your function prototype or function implementation. All three of these elements much match. Second in your function you are using your variable fromyear before you assign a value to this variable. But according to what you provided for your assignment everything should be based on the current year, so you shouldn't even need a year.

    Jim

  3. #3
    Registered User Solarwin's Avatar
    Join Date
    Dec 2012
    Posts
    50
    Quote Originally Posted by jimblumberg View Post
    First your function call doesn't match your function prototype or function implementation. All three of these elements much match. Second in your function you are using your variable fromyear before you assign a value to this variable. But according to what you provided for your assignment everything should be based on the current year, so you shouldn't even need a year.

    Jim
    1.As i am total newcomer about functions, could u give me example how excatly i need to call it in main?
    2.I dont think so, for example, my previous birthday party was in 2012 not 2013 which is current year.

  4. #4
    Cat
    Cat is offline
    Registered User
    Join Date
    May 2003
    Posts
    1,571
    Well, you will need a function that takes day and month (possibly year as well if you want this to work correctly even in leap years).

    Here's a question: how would you (not talking about code here, just talking about the logical process if you were going to compute this yourself on pen and paper) tell me if I asked "How many days in the year is the 5th of March"? Describe how you would go about figuring that out (the answer should be 64, or 65 in a leap year, but how would you go about calculating that?).
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

  5. #5
    Registered User
    Join Date
    May 2010
    Posts
    2,778
    I dont think so, for example, my previous birthday party was in 2012 not 2013 which is current year.
    Then is the following not the correct assignment specifications?
    I have to write a c++ program with my own function which consists of two parametrs (day, month).
    Where in this specification is a year mentioned? Looking further:
    Function have to return number of days since the begining of this year.
    And finally:
    Using this function i have to find out how many days are left till birthday (or how many days have passed since last birthday)
    So if your birth date has already pasted you need to display the number of days passed since your last birthday. But if your birthday hasn't yet occurred you need to display how many days are left before your birthday. No you don't need to know anything about the year, just the day and month are important.

    As i am total newcomer about functions, could u give me example how excatly i need to call it in main?
    You indicate in your instructions that the function should have two parameters, the day and the month so you must implement your function with these two parameters and you must call your function with two parameters.

    Jim


    Jim

  6. #6
    Registered User Solarwin's Avatar
    Join Date
    Dec 2012
    Posts
    50
    Quote Originally Posted by Cat View Post
    Well, you will need a function that takes day and month (possibly year as well if you want this to work correctly even in leap years).

    Here's a question: how would you (not talking about code here, just talking about the logical process if you were going to compute this yourself on pen and paper) tell me if I asked "How many days in the year is the 5th of March"? Describe how you would go about figuring that out (the answer should be 64, or 65 in a leap year, but how would you go about calculating that?).
    Maybe i could use array like this:
    Code:
    int months [12] = {31 , 28  , 31  , 30  , 31  , 30  , 31  , 31  , 30  , 31  , 30  , 31};
    and make function with similar meaning to :

    Code:
    month [3] - enteredDateInDays
    ???
    But then i need that entered date converted in days. dunno how to do that. but i think my teacher want me to make this program with one function.There must be easier way.

  7. #7
    Registered User Solarwin's Avatar
    Join Date
    Dec 2012
    Posts
    50
    Quote Originally Posted by jimblumberg View Post
    Then is the following not the correct assignment specifications?

    Where in this specification is a year mentioned? Looking further:

    And finally:

    So if your birth date has already pasted you need to display the number of days passed since your last birthday. But if your birthday hasn't yet occurred you need to display how many days are left before your birthday. No you don't need to know anything about the year, just the day and month are important.


    You indicate in your instructions that the function should have two parameters, the day and the month so you must implement your function with these two parameters and you must call your function with two parameters.

    Jim


    Jim
    This is the correct assignment. It's my school homework. My teacher wrote that.
    Seems like my teacher also thought that just the day and month are important.
    Could u write me a line for function call in the main?

  8. #8
    Registered User
    Join Date
    May 2010
    Posts
    2,778
    I suggest you give it a try. You seem to have your function prototype correct. When you call your function you must have the same number of parameters as your prototype. What do you think these two parameters should be? If you don't understand functions then I suggest you review your information on functions and maybe study the following tutorials, Functions I and Functions II.

    Jim

  9. #9
    Cat
    Cat is offline
    Registered User
    Join Date
    May 2003
    Posts
    1,571
    Quote Originally Posted by Solarwin View Post
    Maybe i could use array like this:
    Code:
    int months [12] = {31 , 28  , 31  , 30  , 31  , 30  , 31  , 31  , 30  , 31  , 30  , 31};
    and make function with similar meaning to :

    Code:
    month [3] - enteredDateInDays
    ???
    But then i need that entered date converted in days. dunno how to do that. but i think my teacher want me to make this program with one function.There must be easier way.
    I agree with your array (or a similar idea).

    Here's a hint: using your array as you defined it, March 5 would be:

    months[0] + months[1] + 5;

    (That is, add all of January's days plus all of February's plus 5).

    Similarly, June 22nd would be

    months[0] + months[1] + months[2] + months[3] + months[4] + 22;

    (There is a way you could improve your months array contents to make for fewer additions, but the above method is functional).
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

  10. #10
    Registered User Solarwin's Avatar
    Join Date
    Dec 2012
    Posts
    50
    This is how far i am. Looks like months are working, but my if statement is not right in the main. What is the mistake or how to make it right?:/

    Code:
    #include <iostream>
    
    
    using namespace std;
    
    
    int cikDienu(int diena, int menesis);
    int main()
    {
        int dd = 0;
        int mm = 0;
        int dzd = 0;
        int dzm = 0;
        int sodiena =0;
    
    
        cout << "Enter todays date (dd mm): ";
        cin >> dd >> mm;
        cout << "Enter your birthday(dd mm): "<< endl;
        cin >> dzd >> dzm;
    
    
    
    
        if(dzd && dzm < sodiena)
        cout << "Since your birthday have passed " << cikDienu(dd, mm) << " days" << endl;
        else if((dzd && dzm > sodiena))
        cout << "Until your birthday is " << cikDienu(dd, mm) << " days" << endl;
        else
        cout << "Happy birthday!" << endl;
        return 0;
    }
    
    
    int cikDienu(int diena, int menesis){
        int meeneshi [13] = { 0, 31 , 28  , 31  , 30  , 31  , 30  , 31  , 31  , 30  , 31  , 30  , 31};
        int kopa = 0;
    
    
        for(int i = 1; i < menesis; i++ )
            kopa = kopa + meeneshi[i];
    
    
            kopa+=diena;
    
    
        return kopa;
    }
    Last edited by Solarwin; 02-26-2013 at 01:01 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 03-04-2012, 10:17 AM
  2. int value passed to function
    By search in forum C Programming
    Replies: 7
    Last Post: 03-10-2010, 05:05 PM
  3. Replies: 2
    Last Post: 02-26-2009, 10:48 PM
  4. Replies: 3
    Last Post: 11-17-2008, 11:59 AM
  5. Examining flags when passed to a function?
    By DominicTrix in forum C++ Programming
    Replies: 5
    Last Post: 02-01-2003, 12:34 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21