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

Join 131,588 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,971 people online right now. Registration is fast and FREE... Join Now!




diffrences between two dates

 
Reply to this topicStart new topic

diffrences between two dates, c++

castro
post 16 Nov, 2005 - 11:46 AM
Post #1


New D.I.C Head

*
Joined: 12 Nov, 2005
Posts: 9


My Contributions


this code is to determine the number of dyas between two different dates and i hope i did it well. this ny solution



CODE

#include<iostream>
#include<cmath>
using namespace std;
int mon[12]={31,28,31,30,31,30,31,31,30,31,30,31};
void GateDate ();void CalDays();
int yr1,yr2,m1,m2,d1,mons2,mons1,d2,sum=0,yrs,mons,days,j;int const loop = 365;
int main ()
{
GateDate();CalDays();
system ("pause");
return 0;
}
void GateDate()
{
   cout<<"Enter date in the form  DAY - MONTH -YEAR NOTE DATE TWO > DATE ONE \n";
   cout<<" Enter date ONE and press enter to enter month... \n";
   cin>>d1>>m1>>yr1;
   cout<<" Enter date TWO and press enter to enter month... \n";
   cin>>d2>>m2>>yr2;  
}    
void CalDays()
{
                 days = d2 - d1; yrs = yr2 - yr1;
      if (m2>=m1)
      {          mons = m1 - m2;  mons1 = mons; mons2 = abs(mons);
      }
      else
      {          mons = m2 - m1;  mons1 = mons; mons2 = abs(mons);
      }
                 sum = yrs * loop + mons + days + mons2;
  if (m2>m1){ j = m1; }   else { j = m2; }
     if ( mons2 != 0 ){
       for(int i = 0; i < mons2;i++)
        {       sum = sum +mon[j]; j++;  } }              
  cout<<"the number of days in \n"<<d1<<" / "<<m1<<" / "<<yr1<<"\n"<<d2<<" / "<<m2<<" / "<<yr2<<"\n\nis "<<sum + mons <<"\n";
}
User is offlineProfile CardPM

Go to the top of the page


Mrafcho001
post 16 Nov, 2005 - 12:27 PM
Post #2


D.I.C Addict

Group Icon
Joined: 1 Nov, 2005
Posts: 753



Thanked 5 times

Dream Kudos: 120
My Contributions


Works well, but the code organization and structure is very POOR!

I would recommend to start spacing better.
if you have a ; dont put more code behind it, go to another line, it gets very confusing to read.

wink2.gif
User is offlineProfile CardPM

Go to the top of the page

Vextor
post 16 Nov, 2005 - 01:16 PM
Post #3


D.I.C Regular

Group Icon
Joined: 22 May, 2002
Posts: 288



Thanked 1 times

Dream Kudos: 25
My Contributions


Agreed. It makes it easier to read and understand for other's when it's organized. Put space in between functions, declarations etc..

Also try commenting where areas of the program may be complex or hard to understand.

Otherwise it looks good, Keep it up.
User is offlineProfile CardPM

Go to the top of the page

Dark_Nexus
post 16 Nov, 2005 - 03:28 PM
Post #4


or something bad...real bad.

Group Icon
Joined: 2 May, 2004
Posts: 1,309



Thanked 1 times

Dream Kudos: 625
My Contributions


seems like it should work just fine. just as a side note:

the difftime(...) function will return the difference of two times in seconds, and knowing that there are 86400 seconds in a day, the same problem could be solved in this manner as well. This implimentation will only work if you are using two time_t objects.
User is offlineProfile CardPM

Go to the top of the page

castro
post 18 Nov, 2005 - 10:37 AM
Post #5


New D.I.C Head

*
Joined: 12 Nov, 2005
Posts: 9


My Contributions


QUOTE(castro @ 16 Nov, 2005 - 01:43 PM)
this code is to determine the number of dyas between two different dates and i hope i did it well. this ny solution I HAVE EDITED AND GIVEN IT MORE SPACING I HOPE U GET IT THIS TIME.
MY PROBLEM WTIH MY CODE ISW THAT IT DOES NOT WORK FOR SOME DATES



CODE

#include<iostream>
#include<cmath>
using namespace std;
int mon[12]={31,28,31,30,31,30,31,31,30,31,30,31};
void GateDate ();void CalDays();
int yr1,yr2,m1,m2,d1,mons2,mons1,d2,sum=0,yrs,mons,days,j;int const loop = 365;
int main ()
{
GateDate();CalDays();
system ("pause");
return 0;
}
void GateDate()
{
   cout<<"Enter date in the form  DAY - MONTH -YEAR NOTE DATE TWO > DATE ONE \n";
   cout<<" Enter date ONE and press enter to enter month... \n";
   cin>>d1>>m1>>yr1;
   cout<<" Enter date TWO and press enter to enter month... \n";
   cin>>d2>>m2>>yr2;  
}    
void CalDays()
{
                 days = d2 - d1; yrs = yr2 - yr1;
      if (m2>=m1)
      {          mons = m1 - m2;  mons1 = mons; mons2 = abs(mons);
      }
      else
      {          mons = m2 - m1;  mons1 = mons; mons2 = abs(mons);
      }
                 sum = yrs * loop + mons + days + mons2;
  if (m2>m1){ j = m1; }   else { j = m2; }
     if ( mons2 != 0 ){
       for(int i = 0; i < mons2;i++)
        {       sum = sum +mon[j]; j++;  } }              
  cout<<"the number of days in \n"<<d1<<" / "<<m1<<" / "<<yr1<<"\n"<<d2<<" / "<<m2<<" / "<<yr2<<"\n\nis "<<sum + mons <<"\n";
}

#include<iostream>
#include<cmath>
using namespace std;
int mon[12]={31,28,31,30,31,30,31,31,30,31,30,31};

void GateDate ();

void CalDays();

int yr1,yr2,m1,m2,d1,mons2,mons1,d2,sum=0,yrs,mons,days,j;

int const loop = 365;
int main ()
{
GateDate();
CalDays();

system ("pause");
return 0;
}
void GateDate()

{
cout<<"Enter date in the form DAY - MONTH -YEAR NOTE DATE TWO > DATE ONE \n";
cout<<" Enter date ONE and press enter to enter month... \n";

cin>>d1>>m1>>yr1;

cout<<" Enter date TWO and press enter to enter month... \n";

cin>>d2>>m2>>yr2;
}


void CalDays()
{
days = d2 - d1;
yrs = yr2 - yr1;
if (m2>=m1)
{
mons = m1 - m2;
mons1 = mons;
mons2 = abs(mons);
}

else

{
mons = m2 - m1;
mons1 = mons;
mons2 = abs(mons);
}

sum = yrs * loop + mons + days + mons2;

if (m2>m1)
{
j = m1;
}

else
{
j = m2;
}

if ( mons2 != 0 )
{
for(int i = 0; i < mons2;i++)
{
sum = sum +mon[j];
j++;
}
}
cout<<"the number of days in \n"<<d1<<" / "<<m1<<" / "<<yr1<<"\n"<<d2<<" / "<<m2<<" / "<<yr2<<"\n\nis "<<(sum + mons )<<"\n";
}
User is offlineProfile CardPM

Go to the top of the page

Amadeus
post 18 Nov, 2005 - 11:31 AM
Post #6


g++ -o drink whiskey.cpp

Group Icon
Joined: 12 Jul, 2002
Posts: 12,158



Thanked 32 times

Dream Kudos: 25
My Contributions


You mention it does not work for some dates...can you give an example?
User is offlineProfile CardPM

Go to the top of the page

Reply to this topicStart new topic
Time is now: 11/20/08 03:42AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month