How to find how many days to someones bithday

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • cjohns
    New Member
    • Nov 2007
    • 2

    How to find how many days to someones bithday

    I am brand new to programing and am trying to write a C++ program that calculates how many days are left to a persons birthday. I need to make a function for the leap years, and to get number of days in a month, i have no idea how to do this. any help would be great..
    i am using windows op, with visual studios 2005
    this is what i have so far.

    [CODE=cpp]#include <iostream>
    using namespace std;

    int getdate (int& day, int& month, int& year);



    int main ()
    {

    int day, month, year;

    getdate (day, month, year);
    cout << "The date you enterd is:\n";
    cout << day <<endl << month <<endl << year <<endl;



    return 0;
    }


    int getdate (int& day,int& month,int& year)
    {
    cout << "Please enter your birth day\n";
    cout << "Day: ";
    cin >> day;
    cout << "Month (ex 4): ";
    cin >> month;
    cout << "year: ";
    cin >> year;
    return day, month, year;
    }[/CODE]
    Last edited by Ganon11; Nov 24 '07, 07:47 PM. Reason: Please use the [CODE] tags provided.
  • Ganon11
    Recognized Expert Specialist
    • Oct 2006
    • 3651

    #2
    Most of these are hard-coded numbers (i.e. January always has 31 days, there are always 12 months, etc.). You have the problem of deciding whether a given year is a leap year or not (if it is, February has 29 days - if not, February has 28 days), which is a simple mathematical test.

    Now you have a day, month, and year. It should be simple addition/subtraction to find exactly how many days it is from Day A to Day B.

    I'm seeing an array of integers for the days in a month (numbered from 0 to 11), a function isLeapYear(int year) to find out whether year is a leap year, and then a function calculateDays that will either take two Date structs (which you would make yourself), or two sets of month, date, and year integer variables.

    Comment

    • cjohns
      New Member
      • Nov 2007
      • 2

      #3
      so would it look like this? i have yet to do the days in months. and how would i incorporate the bool variable into the program?

      [CODE=cpp]#include <iostream>
      using namespace std;

      int getdate (int& day, int& month, int& year);
      int currentdate (int&day, int& month, int& year);
      int getnumberofdays inmonth (int& month, int& year);
      bool isleapyear (int year);
      int main ()
      {

      int day, month, year;

      getdate (day, month, year);
      cout << "The date you enterd is:\n";
      cout << day <<endl << month <<endl << year <<endl << endl;
      currentdate (day, month, year);
      cout << "The date you enterd is:\n";
      cout << day <<endl << month <<endl << year <<endl << endl;

      return 0;
      }


      int getdate (int& day,int& month,int& year)
      {
      cout << "Please enter your birth day\n";
      cout << "Day: ";
      cin >> day;
      cout << "Month (ex 4): ";
      cin >> month;
      cout << "year: ";
      cin >> year;
      return day, month, year;
      }

      int currentdate (int&day, int& month, int& year)
      {
      cout << "Please enter current date\n";
      cout << "Day: ";
      cin >> day;
      cout << "Month (ex 4): ";
      cin >> month;
      cout << "year: ";
      cin >> year;
      return day, month, year;
      }


      bool IsLeapYear (int year)
      {
      bool rtn;
      rtn = false;
      if ((year % 400) == 0)
      {
      rtn = true;
      }

      else if ((year % 100) == 0)
      {
      rtn = false;
      }

      else if ((year % 4) == 0)
      {
      rtn = true;
      }
      return rtn;

      }[/CODE]
      Last edited by Ganon11; Nov 24 '07, 11:59 PM. Reason: Please use the [CODE] tags provided.

      Comment

      • JosAH
        Recognized Expert MVP
        • Mar 2007
        • 11453

        #4
        Google for "Zeller's congruence" (WFC will hate me for this ;-) That little formula
        calculates a Julian day number for a date; the difference between two dates
        expressed as Julian day numbers is the numerical difference between those numbers.

        kind regards,

        Jos

        Comment

        • Ganon11
          Recognized Expert Specialist
          • Oct 2006
          • 3651

          #5
          Your return statements are not only wrong, but unnecessary.

          You can only return one thing at a time. You cannot return three ints, or five doubles, or multiple anything. You can return one int, one double, a pointer to something, or one struct variable. Thus, saying "return date, month, year;" may be syntactically acceptable, but it's not what you want to do.

          Also, you are passing your variables 'by reference', meaning whatever changes occurring in the function are reflected in the place where they were called (in this case, in main()). So there's no need to return anything - you can make these functions return nothing by replacing "int getdate()..." with "void getdate()..." and deleting your return statement.

          Comment

          Working...