Date and Text

So I am making some basic app and I need to write text for every day between two dates.
Example between 17/1/2019 to 17/1/2020, and I need to connect it to a button but thats a different story.

So my question is: Is there a way to print text for every day between two dates ?

Thank you very much.

So this is the code for getting the days,months and year.

1
2
3
4
5
6
7
8
9
10
11
12
#include<iostream>
#include<ctime>
#include<windows.h>

using namespace std;

int main(){
	SYSTEMTIME a;
	GetLocalTime(&a);
	cout << "Datum: "<<a.wDay<<"/";
	cout << a.wMonth <<"/";
	cout << a.wYear << endl;
I'm not sure if this is the kind of thing you're looking for.

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
#include <iostream>

bool is_leap_year(int year) {
    return year % 4 == 0 && (year % 100 != 0 || year % 400 == 0);
}

int days_in_month(int year, int month) {
    static int days[] = {0,31,28,31,30,31,30,31,31,30,31,30,31};
    if (month == 2 && is_leap_year(year))
        return 29;
    return days[month];
}

struct Date {
    int year, month, day;
    Date(int y, int m, int d) : year(y), month(m), day(d) {}
    bool equal(Date d) const {
        return d.year == year && d.month == month && d.day == day;
    }
    void inc() {
        if (++day > days_in_month(year, month)) {
            day = 1;
            if (++month > 12) {
                month = 1;
                ++year;
            }
        }
    }
};

std::ostream& operator<<(std::ostream& out, const Date& d) {
    return out << d.month << '/' << d.day << '/' << d.year;
}

void print_dates(Date from, Date to) {
    std::cout << from << '\n';
    do {
        from.inc();
        std::cout << from << '\n';
    } while (!from.equal(to));
}

int main() {
    print_dates(Date(2019,1,17), Date(2020, 1, 17));
}

Same using the functionality of <ctime>. Can obviously be extended by parsing the asctime string, or tm struct and using info in indexed array of full day names etc.

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
#include <iostream>
#include <ctime>

const int SECS_TO_DAYS{24 * 60 * 60};
struct tm getDate();

int main()
{
    struct tm start = getDate();
    struct tm finish = getDate();
    
    // CHECK start < finish
    // ... (otherwise swap otherwise
    
    // LOOP - INCLUSIVE
    for (
         struct tm date = start;
         difftime( mktime(&finish), mktime(&date) )/ SECS_TO_DAYS >= 0;
         date.tm_mday++
         )
    {
        std::cout << asctime( &date);
    }
    
    return 0;
}

struct tm getDate()
{
    int year{0}, month{0}, day{0};
    
    std::cout << "Date <year month day>: ";
    std::cin >> year >> month >> day;
    
    struct tm aDate
    {
        .tm_year = year - 1900,
        .tm_mon = month - 1,
        .tm_mday = day
    };
    mktime(&aDate);
    
    return aDate;
}
No it's not that. I will give you an example:

Today is: 24.12.2019,

and when this date occurs I want the program to write some message, for example
"I love you."

so when the date is 25.12.2019 the program will automatically write the second message like
"Today is happy date" ., and so on.

So I want program for the next 365 days in the year, whenever I enter the app, for that day of the year to show me message. So I need 365 messages that I've got to connect with 365 days and only for that day it will show me that one of a kind message.

So the problem is how to connect 365 days starting from "today" with 365 messages

So I need to take the current date from windows and for that day say something.

Thank you.
The following prints the first line in the message file on Jan 1, the second on Jan 2, etc.

day_of_year returns 0 to 364 for non-leapyear days, whether or not it's a leap year. So Mar 1 is always day 59, whether or not it's a leap year. Feb 29 returns the special value 365 (printing the last message in the message file). That way it always prints the same message on the same month/day combination.

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
#include <iostream>
#include <fstream>
#include <ctime>

int day_of_year() {
    time_t tim = time(0);
    tm* tmx = localtime(&tim);
    mktime(tmx);
    if (tmx->tm_mon == 1 && tmx->tm_mday == 29) // Feb 29
        return 365;
    int y = tmx->tm_year + 1900;
    bool isleapyear = y % 4 == 0 && (y % 100 != 0 || y % 400 == 0);
    return tmx->tm_yday - (isleapyear && tmx->tm_mon > 1);
}

void show_daily_message() {
    int day = day_of_year();
    std::ifstream fin("msgs.txt");
    std::string line;
    while (day-- >= 0) std::getline(fin, line);
    std::cout << line << '\n';
}

void create_msg_file() {
    std::ofstream fout("msgs.txt");
    for (int i = 0; i < 366; ++i) fout << "Line: " << i << '\n';
    fout.close();
}

int main() {
    create_msg_file(); // just for testing
    show_daily_message();
}

Last edited on
No it's not that. I will give you an example:

Well, it's not before time. But I can see you were vaguley ob=n the right track to start off with despite a heap of syntax errors, particularly capital letters.

So, you have an array/list/vector of 365 messages? (Yes/No)

Take the current date and time and calculate the day of the year using <ctime> functionality on the tm structure. Then, using that number select the relevant message from a list you prepare - all 365 of them (don't forget leap years).

I reckon that program will take about 2 or 3 lines, plus a handful of code you can copy from the file IO tutorial to read in the message file.

http://www.cplusplus.com/reference/ctime/tm/

We look forward to your code. All of it can be extracted one way or another using the tutorial/reference material on this site.
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
#include <iostream>
#include <fstream>

#include <string>
#include <ctime>

int main()
{
    std::string line;
    std::ifstream read_from_file ("dates_messages.txt");
    
    time_t rawtime;
    time (&rawtime);
    
    struct tm* timeinfo;
    timeinfo = localtime (&rawtime);
    
    size_t count{0};
    if (read_from_file.is_open())
    {
        while ( getline (read_from_file, line) and count < timeinfo->tm_yday)
        {
            count++;
        }
        std::cout << "MESSAGE FOR TODAY:\n" << asctime(timeinfo) << line << '\n';
        
        read_from_file.close();
    }
    else
        std::cout << "Unable to open file";
    
    return 0;
}


Some random messages (Google 'random mess genearator' to play with:

My Mum tries to be cool by saying that she likes all the same things that I do.

The quick brown fox jumps over the lazy dog.

The clock within this blog and the clock on my laptop are 1 hour different from each other.

She folded her handkerchief neatly.

Should we start class now, or should we wait for everyone to get here?

Don't step on the broken glass.

The body may perhaps compensates for the loss of a true metaphysics.

Abstraction is often one floor above you.

She always speaks to him in a loud voice.
The old apple revels in its authority.

If I don’t like something, I’ll stay away from it.

We have never been to Asia, nor have we visited Africa.

I would have gotten the promotion, but my attendance wasn’t good enough.

He didn’t want to go to the dentist, yet he went anyway.
Tough times don’t last. Tough people do. – Robert H. Schuller

Don’t wait for opportunity. Create it.


blah blah blah ... at least 366 required


Topic archived. No new replies allowed.