I wanted to create a program that keeps track of the last time the user watered the plants (manual check). So basically... The user presses a button and the program set today's date as date1, then every day it updates date2. If the difference is bigger than a certain date, the program returns a string.
int main() {
int time_elapsed = ; \\???
std::cout << needs_water(difference) << "\n";
}
Here's the main function, and the called function is the following:
std::string needs_water(int days) {
if (days > 3){
return("Time to water the plant.");
}
else {
return("Don't water the plant!");
}
}
Sorry for my bad English and thanks in advance.
Edit: In a few words what I want to know is how to tell the program how much time elapsed from the last check.
Here's an example that should do what i understood you want to achieve. Replace milliseconds
with days
(c++20) or with hours
#include <iostream>
#include <string>
#include <chrono>
#include <thread>
using namespace std::chrono;
using namespace std::chrono_literals;
template<typename DurationT>
bool time_elapsed(DurationT time) {
static auto last_check = steady_clock::now();
auto now = steady_clock::now();
auto time_passed = now - last_check;
if (time_passed > time){
last_check = now;
return true;
}
else {
return false;
}
}
int main()
{
for(int i=0; i < 12; ++i) {
std::cout << (time_elapsed(milliseconds{3}) ?
"Time to water the plant!" :
"Don't water the plant!") << std::endl;
std::this_thread::sleep_for(1ms);
}
return 0;
}