datetime - c++ get years between date chosen by user and actual date(counting days,months,years) -


i tried doing this:

struct den_t {     int day, month, year; };   int main() {         struct den_t* datum = new struct den_t;         struct den_t* dnes = new struct den_t;      time_t thetime = time(null);     struct tm atime;     localtime_s(&atime, &thetime);      dnes->day = atime.tm_mday;     dnes->month = atime.tm_mon + 1;     dnes->year = atime.tm_yday + 1900;      cin >> datum->day >> datum->month >> datum->year;     if (dnes->year - datum->year >= 18 )         cout << "full aged " << endl;     else         cout << "not full aged " << endl;     system("pause");     return 0; } 

but somehow cant understand what should compare , decrement,could explain me

what else need tell people's date example in float comparing year,month , day of actual time , date user inputs in program?

using these libraries:

http://howardhinnant.github.io/date/date.html

http://howardhinnant.github.io/date/tz.html

this how tackle problem. first code, explanation:

#include "tz.h" #include "date.h" #include <iostream>  int main() {     using namespace date;     using namespace std::chrono;     std::cout << "enter birthday [day month year]:";     int di, mi, yi;     std::cin >> di >> mi >> yi;     if (std::cin.fail())     {         std::cout << "invalid date\n";         return 1;     }     auto y = year{yi};     if (!y.ok())     {         std::cout << "invalid year\n";         return 1;     }     auto m = month(mi);     if (!m.ok())     {         std::cout << "invalid month\n";         return 1;     }     auto d = day(di);     if (!d.ok())     {         std::cout << "invalid day\n";         return 1;     }     auto birthday = y/m/d;     if (!birthday.ok())     {         std::cout << "invalid birthday\n";         return 1;     }     auto local_time = current_zone()->to_local(system_clock::now());     auto today = year_month_day{floor<days>(local_time)};     auto age = today.year() - birthday.year();     if (birthday + age > today)         --age;     if (age >= years{18})         std::cout << "full aged @ " << age.count() << "\n";     else         std::cout << "not full aged @ " << age.count() << "\n"; } 

i first go trouble check validity of user input. have below seems minimum:

  • it must integral input.
  • each integral input must have reasonable value (e.g. month must in range [1, 12].
  • the combination y/m/d must valid date.

a more robust program might give user feedback on input, , give him chance correct mistake.

once assured have valid birthday, need current date in local timezone. this:

auto local_time = current_zone()->to_local(system_clock::now()); 

gets local time.

this local time can converted local year, month , day with:

auto today = year_month_day{floor<days>(local_time)}; 

this computation follows custom birthday begins @ local midnight, regardless of time of day (and on planet) born. in other words, once local year/month/day established, problem independent of local time zone, , local time of day.

next, current age computed:

auto age = today.year() - birthday.year(); if (birthday + age > today)     --age; 

the difference between years of today , birthday first approximation age. approximation refined computing date on birthday falls this year. if year's birthday still in future, custom count 1 year younger. if doing leaned less towards legal system, , more towards scientific work, might compute in other ways, such rounding nearest year (also easy library).

if birthday on feb 29, above code still works: birthday + age result (75% chance) in invalid date, e.g.: feb/29/2015. invalid date correctly compare greater feb/28/2015 , less mar/1/2015, need to! invalid dates friend, not enemy -- have know exist , them.

now simple matter report findings:

if (age >= years{18})     std::cout << "full aged @ " << age.count() << "\n"; else     std::cout << "not full aged @ " << age.count() << "\n"; 

Comments

Popular posts from this blog

php - Invalid Cofiguration - yii\base\InvalidConfigException - Yii2 -

How to show in django cms breadcrumbs full path? -

ruby on rails - npm error: tunneling socket could not be established, cause=connect ETIMEDOUT -