Day, Month Date Calculation.

May 10, 2019 at 5:38pm
This code works.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
  #include <iostream>
#include <string>
using namespace std;



class DayOfYear
{
private:

public:
	 int MonthDays[13] = {0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 };
	 string MonthName[13] = {"", "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" };
	 void print(int);
};



void DayOfYear::print(int day)
{
	int month = 0;

	while (DayOfYear::MonthDays[month] < day)
		month = (month + 1) % 12;

	
	cout << DayOfYear::MonthName[month] << " " << day - DayOfYear::MonthDays[month - 1];
};


int main()
{
	DayOfYear dYear;

	

	int day;
	//Ask user the total day number
	cout << "\nEnter a number you would like to convert into a month and day "<<endl;
	cin >> day;

	//Error check for negative numbers and numbers higher than one year
	if (day <= 0 || day > 365)
	{
		cout << "You must enter a valid number (1 thru 365)" << endl;
	}

	
	dYear.print(day);
	
	cout << endl;
	system("pause");
	return 0;
}
May 10, 2019 at 6:10pm
leap year?
May 10, 2019 at 6:11pm
It doesn’t work for leap years.
May 10, 2019 at 6:19pm
It still tries to print the day even if the input is bad.
May 10, 2019 at 6:44pm
It is a very good effort though.

Add code to compute whether or not you are in a leap year, and get the correct year (either from the user or the current year). Then you can adjust depending on the number of days in February.
Topic archived. No new replies allowed.