Making a Calendar in C++

This is what I have so far. Can someone help me with my if statements, to get February to print with 29 days during a leap year? And also to get the first week of each month aligned correctly?

Here's a screenshot.
http://i.imgur.com/F6zWF.jpg

Like this: https://www.betacalendars.com/uploads/2019/01/February-2020-Calendar.jpg
Last edited on
Yeah, like, where's your C++ code?

URL's to a couple of pretty pictures doesn't tell us you've done any work.
Hello mateopedersen,

The answer is 42. https://www.youtube.com/watch?v=aboZctrHfK8

http://www.cplusplus.com/articles/Ny86b7Xj/

As salem c says with out your code no one has any idea what you have done or what needs fixed.

Andy
Last edited on
Please share some code, OP.
You shouldn't need to enter the weekday of January 1.
You can find it out pretty easily like this.
(Maybe someone will also post a more modern way of doing this.)

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
// find day of week that year y starts on

#include <iostream>
#include <ctime>

int start_weekday_of_year(int y) {
    // set up the tm structure (https://en.cppreference.com/w/c/chrono/tm)
    std::tm t {}; // zero all fields; note that a tm_mon of 0 means January
    t.tm_mday = 1;
    t.tm_year = y - 1900;

    // call mktime on it to normalize the fields, 
    // which includes filling in the weekday value.
    std::mktime(&t);

    return t.tm_wday; // 0 (Sun) to 6 (Sat)
}

int main() {
    const char* const day_names[] {"Sun","Mon","Tue","Wed","Thu","Fri","Sat"};
    for (;;) {
        int y;
        std::cin >> y;
        int d = start_weekday_of_year(y);
        std::cout << "Year " << y << " starts on " << day_names[d] << '\n';
    }
}

Last edited on
Topic archived. No new replies allowed.