Counting days between 2 dates

This is a discussion on Counting days between 2 dates within the C Programming forums, part of the General Programming Boards category; Hello everyone, My program is to count total days between two entered by user dates, during the compilation I am ...

  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    92

    Counting days between 2 dates

    Hello everyone,
    My program is to count total days between two entered by user dates, during the compilation I am getting these errors and do not understand why, and what they mean.
    Here is the errors:
    Code:
    : error C2143: syntax error : missing ';' before 'type'
    : error C2065: 'B' : undeclared identifier
    : error C2224: left of '.day' must have struct/union type
    : error C2065: 'B' : undeclared identifier
    : warning C4013: 'print_date' undefined; assuming extern returning int
    and here is a program;
    Code:
    #include<stdio.h>
    
    int DaysPerMonth[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
    
    struct Sdate //declare structure
    {
    	int month;
    	int day;
    	int year;
    } begin, end;
    
    int isLeapYear(int a)// determine if it is a leap year
    {
    	if((a%4==0&&a%100!=0)||a%400==0)
    	{
    		return 1; //it,s a leap year
    	}
    	else
    		 //not a leap year
    	return 0;
    }
    
    
    struct Sdate EnterDate(void) //function to enter dates
    {
    	int d,m,y;
    	struct Sdate A;
    
    	do
    	{
    		printf("Enter the month: ");
    		scanf("%i", &m);
    		if(m<1||m>12)
    			printf("Invalid entry, try again!\n");
    	}while(m<1||m>12);
    
    	do
    	{
    		printf("Enter the day: ");
    		scanf("%i", &d);
    
    		if(d<1||d>DaysPerMonth[m])
    			printf("Invalid Entry,try again!\n");
    
    	}while(d<1||d>DaysPerMonth[m]);
    
    
    	do
    	{
    		printf("Enter the year (yyyy)");
    		scanf("%i", &y);
    		if(y<0)
    			printf("Invalid entry, try again!\n");
    	}while(y<0);
    
    	printf("\n");
    
    	A.month=m;
    	A.day=d;
    	A.year=y;
    
    	return(A);
    }//end of the function
    
    void swap_dates(void) // function to swap dates
    {
    	struct Sdate temp;
    
    	temp=begin;
    	begin=end;
    	end=temp;
    }
    
    void  validate(void) // validating dates
    {
    	if(begin.year>end.year)
    	{
    		swap_dates();
    	}
    
    	if((begin.year==end.year)&&(begin.month>end.month))
    	{
    		swap_dates();
    	}
    	if(begin.year==end.year&&begin.month==end.month&&begin.day>end.day)
    	{
    		swap_dates();
    	}
    }
    
    int count_days(void)
    {
    	int i;
    	int days=0;
    	int days_per_year=365;
    	
    	for(i=(begin.year+1); i<end.year; ++i)//how many days beetween full years
    	{
    		days=days+(days_per_year+isLeapYear(i));
    	}
    
    	for(i=begin.month; i<=13; ++i)//how many days in a beginning year+ already counted days 
    	{
    		if(begin.year%4==0&&begin.year!=100&&begin.month==2)
    		{
    			days+=(DaysPerMonth[i]+1);
    		}
    		else
    			days+=DaysPerMonth[i];	
    	}
    
    	for(i=1; i<=end.month; ++i)//how many days in a ending year+ already counted days
    	{
    		if(end.year%4==0&&end.year!=100&&end.month==2)
    		{
    			days+=(DaysPerMonth[i]+1);
    		}
    		else
    			days+=DaysPerMonth[i];	
    
    	return days; //returning total days
    }
    
    void print_date(struct Sdate B) 
    {
    	printf("%i/", B.month);
    	printf("%i/", B.day);
    	printf("%i\n", B.year);
    }
    
    int main(void)
    {
    	begin=EnterDate();
    	end=EnterDate();
    
    	validate();
    
    	print_date(begin);
    	print_date(end);
    	
    	printf("Total days between these two dates is %i\n", count_days());
    }
    Can anyone explain what is wrong.
    Thank you.

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    You are missing an end-brace.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Oct 2008
    Posts
    92
    OMG! Thank you very much matsp!

  4. #4
    Registered User
    Join Date
    Oct 2008
    Posts
    92
    Now I am having a trouble with count_days function, it gives wrong result. To me the calculation seems to be right, but according to result it's not. Actually I was counting all days from beging to the end for first year, so if date entered by user was 2/15/2004, I was counting how many days passed from 1/1/1- to 2/15/2004 same thing with ending year. Then I just had to find different of days between those days.

    here si the function
    Code:
    long int count_days(void)
    {
    	int A[13]={ 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 };
    	int B[13]={ 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366 };
    	int a, b;
    	int i, count, leap_count=0;
    	long int days1=0, days2=0, total_days;
    	int days_per_year=365;
    	
    	for(i=1; i<begin.year; ++i)//how many days in a full years before last begining year
    	{
    		count=isLeapYear(i);
    		leap_count+=count;
    		
    		days1+=days_per_year;	
    	}
    	days1+=leap_count;// adding amount of leap years passed from year 1 to (begin.year-1)
    
    	leap_count=0;
    
    	for(i=1; i<end.year; ++i) //how many days in a  in a full  years before last end year
    	{
    		count=isLeapYear(i);
    		leap_count+=count;
    		
    		days2+=days_per_year;
    	}
    	days2+=leap_count; //adding amount of leap years passed from ...
    
    	a=isLeapYear(begin.year);
    	b=isLeapYear(end.year);
    
    	if(a==1) // if egin year isleap year
    	{
    		days1=days1+B[begin.month-1]+begin.day; // amount of days before begin.year + days past in a begin.year
    	}
    	else if(b==1) // if end year is leap year
    	{
    		days2=days2+B[end.month-1]+end.day;	
    	}
    	else // if not leap year
    	{
    		days1=days1+A[begin.month-1]+begin.day;
    		days2=days2+A[end.month-1]+end.day;
    	}
    	total_days=days2-days1; // amount of days between first and second years
    
    	return total_days;
    
    }
    Please, any sugestions.

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You could also do this with an array of the days in that month, and a loop. First, subtract the start day of the starting month from the days in that month. Then while you aren't at the right year add each years worth of days (allowing for leap year). Once you're in the right year, while you're not at the right month, add those months' day count in. Then add the date in the last month.

    Quzah.
    Hope is the first step on the road to disappointment.

  6. #6
    Registered User
    Join Date
    Oct 2008
    Posts
    92
    Thank you I'll try :=)

  7. #7
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    It almost looks like you're trying to recreate something that's already in the standard C library. i.e. the tm struct and some of the time related functions: Standard C Date & Time [C++ Reference]
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  8. #8
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    By all means save yourself a lot of work if it's allowed, and use the built in functions for this.

  9. #9
    Registered User
    Join Date
    Oct 2008
    Posts
    92
    Thank you guys, I wasn't allowed to use any library except stdio.h.
    I am done with this program, I deleted that function and wrote another one which is actually came out to be simpler and it's working.
    Thanks again for your support.

  10. #10
    Registered User
    Join Date
    Feb 2010
    Posts
    2
    nynicue:
    please post your latest working program.

    Counting days between 2 dates
    Thank you.
    jukinch

  11. #11
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Jukinch, the thread you posted into is 10 months old. I'm sure the original poster is not around any more.

    If you have a question or problem with your program, it's much better to start a new thread.

  12. #12
    Registered User
    Join Date
    Feb 2010
    Posts
    2

    Thumbs up

    Thanks adak. I will first study a little the issue and then publish a new post.

    Jukinch

  13. #13
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    Quote Originally Posted by jukinch View Post
    nynicue:
    please post your latest working program.

    Counting days between 2 dates
    Thank you.
    jukinch
    Maybe, just maybe, you should do your homework for yourself...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. number of days between 2 dates.
    By explosive in forum C++ Programming
    Replies: 10
    Last Post: 02-17-2005, 07:30 AM
  2. days between dates formula
    By maes in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 07-12-2004, 01:52 PM
  3. Counting days
    By TheShaggmeister in forum C++ Programming
    Replies: 5
    Last Post: 07-17-2003, 11:29 AM
  4. Counting Number of days from year zero
    By wireless in forum C++ Programming
    Replies: 4
    Last Post: 06-16-2002, 08:31 AM
  5. Calculating days between two Dates
    By Niy in forum C++ Programming
    Replies: 9
    Last Post: 03-12-2002, 08:16 PM

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