c++ - How to get current system time and cast in array? -
i want time , date separately put array. calling function int date(int day,int month,int year)
isnt correct. how can define arguments year month day , hour min sec of function use them ?
using namespace std; int date() { time_t currenttime; struct tm *localtime; time(¤ttime); localtime = localtime(¤ttime); int day = localtime->tm_mday; int month = localtime->tm_mon + 1; int year = localtime->tm_year + 1900; return (0); } int time() { time_t currenttime; struct tm *localtime; time(¤ttime); localtime = localtime(¤ttime); int hour = localtime->tm_hour; int min = localtime->tm_min; int sec = localtime->tm_sec; return (0); } int main() { unsigned int new_date=date(); char write[4]; memcpy(write,&new_date,4); unsigned int new_time=time(); char wrt[4]; memcpy(wrt,&new_time,4); }
you have declare function pass arguments reference:
// pass arguments reference change them in function int date(int &day, int &month, int&year) { time_t currenttime; struct tm *localtime; time(¤ttime); localtime = localtime(¤ttime); day = localtime->tm_mday; // use reference argument month = localtime->tm_mon + 1; year = localtime->tm_year + 1900; return (0); }
you can use in main()
:
int d,m,y; date(d,m,y); cout << d<<"/"<<m<<"/"<<y<<endl;
you can of course same thing time.
Comments
Post a Comment