Hello,

I am having some trouble figuring out an assignment I was given in class. I am not looking for any code, but need a better explanation that what the teacher has given. Basically the program will calculate the number of days between any two days from 1/1/1900 through 12/31/2099, both dates will be entered by the user. I understand the algorithm and process, but am having a difficult time translating that into C++. I am not looking for any code, but maybe a better explanation of how to approach this program. I will be calling a function twice for the dates, and will use 3 different arrays to store the days in the month/leap year days etc.

Any information is greatly appreciated.

Thanks

If you understand the algorithm and the process then you know how to program it. Why not write out your algorithm and where exactly you get stuck (ie- write the pseudo-code out)

Enter date #1 and #2
Break both dates into day,month,year values.
Start with that and get it working. Then start the meat of the project.

#include <iostream>
#include <cmath>
using namespace std;

int main()
{
    int fdate;
	int sdate;
	int diff;

	cout << "Please enter the first date: " << endl;
	cin >> fdate;

		if ( fdate < 1900 || fdate > 2099 ) 
			cout << "Please enter a year between 1900 and 2099: " << endl;
			cin >> fdate;

	cout << "Please enter the second date: " << endl;
	cin >> sdate;

		if ( sdate < 1900 || sdate > 2099 ) 
			cout << "Please enter a year between 1900 and 2099: " << endl;
			cin >> sdate;

	


	system("pause");
	return 0;

}

Hey thanks for the reply, here is what I have so far, which isn't much but...Is it best to declare the arrays before the main? And I am still confused on functions...

You've entered the year, but not the day or the month. You'll need that information, too. That means you'll need 6 variables to do the calculation. They can all be independent variables, or you can declare a date class with each date object having a day, month and year as member variables.

You've entered the year, but not the day or the month. You'll need that information, too. That means you'll need 6 variables to do the calculation. They can all be independent variables, or you can declare a date class with each date object having a day, month and year as member variables.

Would you mind showing me an example of a class? I would insert the class before my main making it global right?

Pretty straightforward for something like this:

struct 
{
   int day;
   int month;
   int year;
};

However, if you don't know the syntax for using that, maybe you better stick with six independent variables. Then when you learn about classes from your instructor you can come back and do it with that syntax.

Pretty straightforward for something like this:

struct 
{
   int day;
   int month;
   int year;
};

However, if you don't know the syntax for using that, maybe you better stick with six independent variables. Then when you learn about classes from your instructor you can come back and do it with that syntax.

Ahhh...dur. Thanks. I will work on it today and post again. Thanks for the help.

I have made some changes, and have some of the concepts figured out, but still confused on how I determine "numofdays" and the leap year functionality.

#include <iostream>
#include <cmath>
using namespace std;

int main()
{
    int month;
	int month2;
	int day;
	int day2;
	int year;
	int year2;
	numofdays = 0;
	jdate = 0;

	const int daysInYear [] = {0, 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365};
	const int daysInLeapYear[] = {0, 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366};
	const int daysInMonth [] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
	const int daysInMonthLeap [] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

	cout << "Please enter a date (ex. 4 28 1986): " << endl;
	cin >> month >> day >> year;

	if ( month < 0 || month > 12 || day < 0 || day > 31 || year < 1900 || year > 2099 )
		{
			cout << "Please enter year after 1900 or before 2099: " << endl;
			cin >> month >> day >> year;
		}

	cout << "Please enter another date (ex. 4 28 1986): " << endl;
	cin >> month2 >> day2 >> year2;

	if ( month2 < 0 || month2 > 12 || day2 < 0 || day2 > 31 || year2 < 1900 || year2 > 2099 )
		{
			cout << "Please enter year after 1900 or before 2099: " << endl;
			cin >> month2 >> day2 >> year2;
		}
	
	jdate = year - 1900;
	jdate = year * 365 +((year-1) / 4) + daysInYear[month] + day

	cout << month << day << year << endl;
	cout << month2 << day2 << year2 << endl;
	cout << "The number of days between these dates is: " << numofdays;
	
	system("pause");
	return 0;

}

When i try to compile I get jdate and numofdays are undeclared identifiers?

Where are they actually declared? I see where you set them to values, but you don't declare them anywhere like you did month day, year.

Where are they actually declared? I see where you set them to values, but you don't declare them anywhere like you did month day, year.

Ok, I forgot to add the data type to those two values. How do I find out what numofdays is? In terms of solving the equation?

By calculating it? At least that's what I'd do.

What definition of leap year are you going to use. the simple one is any year evenly divisible by 4, but there are more sophisticated, and accurate, definitions as well.

On the other hand, developing an algorhithm to caclculate difference between days ignoring leap years first, and then modifying that to account for leap years sounds like a better strategy.

What definition of leap year are you going to use. the simple one is any year evenly divisible by 4, but there are more sophisticated, and accurate, definitions as well.

On the other hand, developing an algorhithm to caclculate difference between days ignoring leap years first, and then modifying that to account for leap years sounds like a better strategy.

I am officially stuck, I cannot figure out how to get the amount of days from the two dates entered?

Can you tell us how many days between Mar 23 and June 5 of this year?
Don't use the computer, just figure it out.

Now do it again, but write down each step as you do it, and post it here.

Can you tell us how many days between Mar 23 and June 5 of this year?
Don't use the computer, just figure it out.

Now do it again, but write down each step as you do it, and post it here.

Thanks for your help...I finally figured it out, and of course I was making it more difficult then it was. I subtracted the date entered from 1900 then indexed the array depending on what the user entered for the month.

#include <iostream>
#include <cmath>
using namespace std;

int main()
{
    int month,month2,day,day2,year,year2,jdate,jdate2,diff;

	const int daysInYear [] = {0, 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365};
	const int daysInLeapYear[] = {0, 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366};
	const int daysInMonth [] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
	const int daysInMonthLeap [] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

	cout << "Please enter a date (ex. 4 28 1986): " << endl;
	cin >> month >> day >> year;

	if ( month < 0 || month > 12 || day < 0 || day > 31 || year < 1900 || year > 2099 )
		{
			cout << "Please enter year after 1900 or before 2099: " << endl;
			cin >> month >> day >> year;
		}

	cout << "Please enter another date (ex. 4 28 1986): " << endl;
	cin >> month2 >> day2 >> year2;

	if ( month2 < 0 || month2 > 12 || day2 < 0 || day2 > 31 || year2 < 1900 || year2 > 2099 )
		{
			cout << "Please enter year after 1900 or before 2099: " << endl;
			cin >> month2 >> day2 >> year2;
		}

	jdate = (year - 1900) * 365 + daysInYear[month] + day;
	jdate2 = (year2 - 1900) * 365 +daysInYear[month2] + day2;
      
  	diff = abs( jdate - jdate2);

	cout << "The number of days between these dates is: " << diff << endl;
	
	system("pause");
	return 0;

}

I am still stuck on one formula. I know how to calculate the leap year but not sure where in my code to check the year? I was thinking of using the following:

if ( year % 4 && year !=0 || year % 100 &&  !=0 || year % 400 && !=0 )

Are you aware that there are member functions in the ctime library that can help you with this. I forget exactly what they are but it would make your life a lot easier.

Are you aware that there are member functions in the ctime library that can help you with this. I forget exactly what they are but it would make your life a lot easier.

Ya, but we were told not to use them. Have to figure it out the long way.

Ya, but we were told not to use them. Have to figure it out the long way.

Oh, that sounds like a lot more fun :-)

You use that if statement to test each year from your start year to your end year, and if TRUE, add one day to your total.

Keep in mind for the first year, if the date starts March 1 or later you don't check that year.

Also for the last year if the date is Feb 28 or earlier, don't check that year either.

Hi,

I was looking for an algorithm to calculate the number of days between two days and found this very helpfull thread. I am just a C++ beginner and have one problem: How would the code look like if I want to define the procedure for a function? I mean how do I have to replace lines 14, 15, 23 and 24?

I want to define the function DiffDatum(char Datum1, char Datum2). Datum1 and Datum2 are in the Format DD/MM/YYYY and I want the number of days between Datum1 and Datum2.

Thanks a lot for your help
Jérôme

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.