patterncppMinor
Day counter between two dates
Viewed 0 times
datestwobetweencounterday
Problem
I wrote this little program, but I'm not sure if it's properly written. Actually, it can't handle leap years (every year is 365 days long) and you have to write the earlier date before. It seems to be working correctly.
Can you tell me if it's okay or not, and what should I change in it to make it good?
Can you tell me if it's okay or not, and what should I change in it to make it good?
#include
using namespace std;
int first_date_month;
int first_date_days;
int first_date_year;
int second_date_month;
int second_date_days;
int second_date_year;
int days;
int month_days[] = {31,28,31,30,31,30,31,31,30,31,30,31};
int main()
{
cout > first_date_year >> first_date_month >> first_date_days;
cout > second_date_year >> second_date_month >> second_date_days;
if(first_date_year == second_date_year)
{
if(first_date_month == second_date_month)
days = second_date_days - first_date_days;
else
{
for(int i = first_date_month; i = 0)
days += (second_date_year - first_date_year - 1)*365 +
month_days[first_date_month-1] - first_date_days + second_date_days;
}
cout << "Days between the two dates: " << days;
return(0);
}Solution
-
Do not use
-
Never use global variables. Right away, this will introduce all sorts of problems, including maintainability and bugs. There are different alternatives to this, one being a
Such a structure just needs the month, day, and year:
Initialize the two
Access the structures and set the data members (with your code):
If you want to get into encapsulation/information-hiding, I'd recommend a
-
I don't like
-
To account for leap-years, I'd either:
With that, you can allow the program to adjust February's value if a leap-year.
Do not use
using namespace std in global scope.-
Never use global variables. Right away, this will introduce all sorts of problems, including maintainability and bugs. There are different alternatives to this, one being a
struct:Such a structure just needs the month, day, and year:
struct Date
{
int month;
int day;
int year;
};Initialize the two
Date instances:// same order as appears in struct
Date date1 = {1, 2, 2000};
Date date2 = {4, 5, 2001};Access the structures and set the data members (with your code):
std::cout > date1.year >> date1.month >> date1.day;
std::cout > date2.year >> date2.month >> date2.day;If you want to get into encapsulation/information-hiding, I'd recommend a
class instead. If you want to keep this simpler than a struct, just move the globals into main() (but use the variables from my example). You could also create more specialized functions, thereby not just working in main(). Modularity will help keep your code more organized and maintainable.-
I don't like
int for these values (dates cannot be negative). I'd go with std::size_t instead (include ` to use it).
-
month_days[] should be a const` while in global scope. As a constant, it can remain there because it cannot be changed by anything else. However, this will prevent you from accounting for leap-years. Speaking of which...-
To account for leap-years, I'd either:
- leave out February's value from the array (it's the only value that could change)
- not make the array a constant (the program will handle the values during runtime)
With that, you can allow the program to adjust February's value if a leap-year.
Code Snippets
struct Date
{
int month;
int day;
int year;
};// same order as appears in struct
Date date1 = {1, 2, 2000};
Date date2 = {4, 5, 2001};std::cout << "Enter first date: ";
std::cin >> date1.year >> date1.month >> date1.day;
std::cout << "Enter second date: ";
std::cin >> date2.year >> date2.month >> date2.day;Context
StackExchange Code Review Q#30448, answer score: 8
Revisions (0)
No revisions yet.