Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 136,179 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 2,001 people online right now. Registration is fast and FREE... Join Now!




c++ - Dates - Advancing day by day

 
Reply to this topicStart new topic

c++ - Dates - Advancing day by day

penny222
30 Sep, 2007 - 06:59 PM
Post #1

New D.I.C Head
*

Joined: 30 Sep, 2007
Posts: 2


My Contributions
Need to write a function that advances a date one day at a time until it matches a second date. I coded everything else, just having problems with this part of the problem.

One function is passed two dates and then a function within that function does the advancing one day at a time, keeping count along the way and then returning the number of days in between the two dates.

Please help.

Thanx
User is offlineProfile CardPM
+Quote Post

jjhaag
RE: C++ - Dates - Advancing Day By Day
30 Sep, 2007 - 07:04 PM
Post #2

me editor am smartastic
Group Icon

Joined: 18 Sep, 2007
Posts: 1,789



Thanked: 2 times
Dream Kudos: 775
Expert In: C,C++

My Contributions
show your code
User is offlineProfile CardPM
+Quote Post

penny222
RE: C++ - Dates - Advancing Day By Day
30 Sep, 2007 - 07:12 PM
Post #3

New D.I.C Head
*

Joined: 30 Sep, 2007
Posts: 2


My Contributions
QUOTE(jjhaag @ 30 Sep, 2007 - 08:04 PM) *

show your code

CODE

#include<iostream>
#include<fstream>
#include <cstdlib>
#include<cctype>
#include<cmath>
#include<string>
#include<cstring>
#include<iomanip>
using namespace std;


class Date
{
    
    private:
        int month;
        int day;
        int year;
    
    public:
        
        
        Date(int mm, int dd, int yy);            
        Date( ){month=1; day=1; year=1900;}                
        
        int GetDay();
        int GetMonth();
        int GetYear();
        
        void set_date(int mm, int dd, int yy);    
        void display_date();                    
                                
        bool is_equal(Date& );
        bool is_less_than(Date& );
        
        void advance();                            /
};


int difference(int earlier_day, int earlier_month, int earlier_year, int later_day, int later_month, int later_year, Date date1, Date date2);


int main()
{
    
    int m1, m2, d1, d2, y1, y2, index=0;
    int mm1, mm2, dd1, dd2, yy1, yy2, days_apart;

    Date First_Date, Second_Date, earlier_date, later_date;

    cout<<"This program gives the number of days between two dates."<<endl;

    for (index=0; index<4; index++)
    {

    cout<<"Please enter a date in the form xx/xx/xxxx"<<endl;
    
    m1=First_Date.GetMonth();
    d1=First_Date.GetDay();
    y1=First_Date.GetYear();
    
    First_Date.set_date(m1, d1, y1);

    cout<<"First Date is:"<<endl;
    First_Date.display_date();
    cout<<endl;

    cout<<"Please enter a second date in the form xx/xx/xxxx"<<endl;
    
    m2=Second_Date.GetMonth();
    d2=Second_Date.GetDay();
    y2=Second_Date.GetYear();
    
    Second_Date.set_date(m2, d2, y2);

    cout<<"Second Date is:"<<endl;
    Second_Date.display_date();
    cout<<endl;

    if(First_Date.is_equal(Second_Date))
    {
        cout<<"The first date you entered is the same as the second date."<<endl;
        
    }

    else if(First_Date.is_less_than(Second_Date))

    {
        
        earlier_date=First_Date;        
        later_date=Second_Date;
        
        cout<<"The first date you entered is earlier than the second date."<<endl;


        dd1=d1;
        mm1=m1;
        yy1=y1;
        
        dd2=d2;
        mm2=m2;
        yy2=y2;
        
    }

    else
    {    
        cout<<"The first date you entered is later than the second date"<<endl;
        
        earlier_date=Second_Date;
        later_date=First_Date;
        

        dd1=d2;
        mm1=m2;
        yy1=y2;
        
        dd2=d1;
        mm2=m1;
        yy2=y1;

    }

    days_apart=difference(dd1, mm1, yy1, dd2, mm2, yy2, earlier_date, later_date);
    cout<<"The two days are "<< days_apart<<" days apart."<<endl;
    }

    return 0;
}



difference(Date date1, Date date2)
{

    int diff_days=0;

    while ((earlier_day!=later_day)|| (earlier_month!=later_month) || (earlier_year!=later_year))
    
    {
        date1.advance();
        diff_days++;
    }
    return diff_days;

}


void Date::advance()

{
        
    int months[12];

    int max_no_months=12;

    int days_in_month[12]={31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};



    
}






int Date::GetMonth()
{
    char slash;
    int month_of_year;
    cin>>month_of_year;
    cin>> slash;
    return month_of_year;
}


int Date::GetDay()
{
    char slash;
    int day_of_year;
    cin>>day_of_year;
    cin>> slash;
    return day_of_year;
}

int Date::GetYear()
{
    int year_after_1900;
    cin>>year_after_1900;
    return year_after_1900;
}


void Date::set_date(int mm, int dd, int yy)
{    
    month = mm;
    day = dd;
    year = yy;
    
}

void Date::display_date()
{    
    
    string name_of_month[12];

    name_of_month[0]="January";
    name_of_month[1]= "February";
    name_of_month[2]= "March";
    name_of_month[3]= "April";
    name_of_month[4]=  "May";
    name_of_month[5]= "June";
    name_of_month[6]= "July";
    name_of_month[7]= "August";
    name_of_month[8]= "September";
    name_of_month[9]= "October";
    name_of_month[10]= "November";
    name_of_month[11]= "December";

    cout <<  name_of_month[month-1]  <<  " "  <<  day  <<  ", "  <<   year;
    return;
}

bool Date::is_equal(Date& Second_Date)
{
    
    if ((month==Second_Date.month) && (day==Second_Date.day) && (year==Second_Date.year))
        {return true;}

    else
        {return false;}
}

bool Date::is_less_than(Date& Second_Date)
{

    if (year<Second_Date.year)
    {
        return true;
    }

    else if (month<Second_Date.month)
    {
        return true;
    }
    else if (day<Second_Date.day)
    {    
        return true;
    }
    
    else
    {    
        return false;
    }    
}

User is offlineProfile CardPM
+Quote Post

c++beginner123
RE: C++ - Dates - Advancing Day By Day
5 Oct, 2008 - 12:20 PM
Post #4

New D.I.C Head
*

Joined: 5 Oct, 2008
Posts: 5

Pls let me know if you got the answer
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/2/08 01:02AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month