Hi, I'm new here and also new in programming.
I have a home assigment to make a c++ program which calculates the difference between two dates in days. (present - dateOfBirth).
I don't think I'm allowed to use functions already implemented in c++, but my own.
I've found many examples on the web and here some but not exactly what I need.
I've came across this (the comments in the code are my concerns) I don't understand some simple things, also can the daysFrom1900 function be simplified? where is the 1900 limitation?
using namespace std;
int daysInMonth (int month);
int leapYear (int year);
int daysFrom1900 (int year, int month, int day, int year2, int month2, int day2);
int main()
{
int year, year2, month, month2, day, day2;
cout << "Enter a year: ";
cin >> year >> month >> day >> year2 >> month2 >> day2;
int counter = daysFrom1900(year, month, day, year2, month2, day2);
cout << counter;
system("pause");
return 0;
}
int leapYear (int year)
{
int a = 365;
if( year % 4 == 0 ){
if(year % 100 == 0 && year % 400 != 0){
FALSE;
}
TRUE;
}
FALSE;
} // Why year%100 and year % 400?? also "a" is never used.
int daysInMonth (int month)
{
int days;
switch (month){
case 1 : days = 31; break;
case 2 : /*leapYear(year)? days = 29 :*/ days = 28; break;
case 3 : days = 31; break;
case 4 : days = 30; break;
case 5 : days = 31; break;
case 6 : days = 30; break;
case 7 : days = 31; break;
case 8 : days = 31; break;
case 9 : days = 30; break;
case 10 : days = 31; break;
case 11 : days = 30; break;
case 12 : days = 31; break;
}
return days;
} // can I use if statement in case 2? I mean: if(leapyear(year)) days=29; else days=28;
int daysFrom1900 (int year, int month, int day, int year2, int month2, int day2)
{
int y, m, d, y2, m2, d2;
y = year;
y2 = year2;
m = month;
m2 = month2;
d = day;
d2 = day2;
int counter = 0;
if (y == y2)
for ( int a = 1; a <= m2; a++)
if (a == m2){
for (int b = 1; b <= d2; b++)
q++;
}//who is "q" ?
else {
for (int b = 1; b <= daysInMonth(a); b++)
counter++;
}
else
for (int c = y; c <= y2; c++){
if (c == y){
for ( int a = m; a <= 12; a++)
for (int b = d; b <= daysInMonth(a); b++)
counter++;
}
else if (y == y2){
for ( int a = 1; a <= m2; a++)
if (a == m2){
for (int b = 1; b <= d2; b++)
counter++;
}
else
for (int b = 1; b <= daysInMonth(a); b++)
counter++;
}
else if (c != y){
for ( int a = 1; a <= 12; a++)
for (int b = 1; b <= daysInMonth(a); b++)
counter++;
}
}
return counter - 1;
}//didn't understood much :(
I also found some code but with classes. I'm supposed to learn about classes in a week or two.
I just want simple c++ code, beginner style.
Can somebody please clarify these things for me? Thanks in advance.