Hi all, I have been working on this code all day and can't seem to really get a grasp on it. Essentially I need to calculate the number of days in the year when the user inputs a month (by name), day, and year
My output would look something like this:
<< Enter the (Month Date, Year) i.e. March 23, 1999:
>> january 1, 2009
<< the Julian date is 1
<< Enter the (Month Date, Year) i.e. March 23, 1999
>> december 31, 2000
<< the Julian date is 366
It has to take into account leap years.
This is the code that I have so far. Any direction would be very much appreciated. I'm having trouble trying to visualize it.
#include <iostream>
#include <string>
#include <cstdlib>
using namespace std;
int main()
{
int daysInMonth[12] = {0, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
int leapYear[13] = {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
char month[10];
int day;
int year;
int i, x, mm, sum;
cout << "Enter the (Month Date, Year) i.e. March 23, 1999: ";
cin >> month >> day >> year;
strlwr(month);
if( (year % 4 == 0 && year % 100 != 0) || year % 400 == 0) // leap year
daysInMonth[1] = 29;
else
daysInMonth[1] = 28;
x = strcmp(month, "january");
if(x == 0)
mm = 1;
else if(strcmp(month, "february") == 0)
mm = 2;
else if(strcmp(month, "march") == 0)
mm = 3;
else if(strcmp(month, "april") == 0)
mm = 4;
else if(strcmp(month, "may") == 0)
mm = 5;
else if(strcmp(month, "june") == 0)
mm = 6;
else if(strcmp(month, "july") == 0)
mm = 7;
else if(strcmp(month, "august") == 0)
mm = 8;
else if(strcmp(month, "september") == 0)
mm = 9;
else if(strcmp(month, "october") == 0)
mm = 10;
else if(strcmp(month, "november") == 0)
mm = 11;
else if(strcmp(month, "december") == 0)
mm = 12;
sum = 0;
// counter to add days in the month for the date
for(i=0; i < mm; i++)
sum = day + daysInMonth[i];
cout << endl << sum << endl;
return 0;
}
I keep getting hung up on the leap year and the counter to add to the input day the number of days prior to what I input.
I'd really appreciate any help at all.