Basic calendar

So, I'm attempting to create a calendar using basic C++ concepts.
Honestly, I've only been doing this for about 5 weeks now, and a lot of the basics of C++ are still giving me trouble.
Anyway, I'm shooting for creating a calendar that takes the user input of the year and what day of the week that specific year starts on then outputs a single year calendar. I know it needs take into account leap years.

This is as far as I've gotten, and I'm just kind of confused as to which step I should take next.

I know I need to somehow take out the month input, because I put that in before I refined my requirements, but I'm not sure how to without losing my leap year equations!

Thank you for your help!


I have just started. I want to get a printout like this. ( https://www.dreamcalendars.com/calendar/january-2020 )

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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
  #include <iostream>
#include <cmath>
using namespace std;

int main()
{
    int month, year;
    int first_day, n_days;
    
    
	cout << "Enter the year:";
      cin >> year;
	cout << endl;

	cout << "Enter the month:";
		cin >> month;
		cout << endl;
    
      cout << "Enter the first day of the year (1 for Sunday, 2 for Monday, etc.): ";
      cin >> first_day;

    
    switch (month)
    {
       case 2:  

if((year%400==0) || (year%4==0 && year%100 !=0))

	n_days = 29;

else
		n_days = 28;
    
break;
       case 4:
       case 6:
       case 9:
       case 11:
		   n_days = 30;
		   break;
	  case 1:
      case 3:
      case 5:
      case 7:
      case 8:
      case 10:
      case 12:
            n_days = 31;
            break; 
	}
            

    
    cout << endl << "Sun\tMon\tTue\tWed\tThr\tFri\tSat\n";
    for (int i = 1; i < first_day; i++)
    {
        cout << " \t";
    }
    for (int j = 1; j <= n_days; j++)
    {
        if (((j + first_day - 2) % 7 == 0) && (j != 1))
            cout << endl;
        cout << j  << "\t";
    }
    cout << endl << endl;

    return 0;
}
Last edited on
I'm just kind of confused as to which step I should take next

So your question is how to ask questions to make decisions?

I just ran your routine and it looks nice. Only one thing is questionable, why do you query for the first day of year? 1/1/1900 was a Monday and every seventh day after that also.

A better idea would be to ask the user about the first day of week, many regard Monday as the beginning of a week.
Sunday is the starting day because I will design the calendar according to American standards.
You can use mktime to get first_day. This has the advantage of handling months before October 1582 when the Gregorian calendar came into use.
http://www.cplusplus.com/reference/ctime/mktime/
https://en.wikipedia.org/wiki/Gregorian_calendar
Topic archived. No new replies allowed.