Number of Days between Two Dates

Hello,
So I've been trying to figure out how to do this program. I know there are many mistakes. So I'm sorry about that.
The program will calculate and print out the total number of days between two dates. The accepted dates are between 1/1/1901 and 12/31/2099. The program should have no limitation that the first date must be earlier or later than the second date.

What I did was create a readDate function with three reference parameters which would validate the dates and print an error message if invalid. I also created a second function to calculate the total number of days, which is then returned.
I know I'm doing something wrong with the second function.


Last edited on
This program could be organized in a much better way.

1. Create a function
int MonthI(string monthName)

which accepts the name of month and returns its number (0-11)

2. Create a function

bool IsLeapYear(int year)

which returns true when the given year is leap year

3. Create a function
int DaysInMonth(int monthI, bool isLeapYear)

which returns the number of days in a given month.

Write these functions, test them, and them come back here for more help if you need it.

Last edited on
This problem is in a textbook which I wanted to try out. It states to just use those two functions.
That seems to me as a bad advice. It is so bad that I refuse to solve this problem in that manner.

Does it explicitly say to use only those two functions? Or does it say to use those two functions, and perhaps some others?

Post the text of the exercise.
Last edited on
closed account (48T7M4Gy)
1
2
3
4
5
6
7
8
9
string readDate (int& m, int& d, int& y)
{
   //bla bla
   if (d < 0 || d > 31)
   {
      cout << "Sorry, day is invalid." << endl;
   }
   d = (y - 1901)*365 + (y - 1901)/4;
}


This snippet is a bit concerning. The way it reads to me is you have a number of days d, you check that d is between 0 and 31 inclusive, and then go on to recalculate d.

Why not just recalculate it and forget about deciding via the if statement that goes nowhere?

Is d the number of days or the day number, in which case the lower bound is 1 not 0.
Topic archived. No new replies allowed.