User input date

i want a user to enter a date than get the week number so far i know how to get the week number by using strftime(date,3,"%W",&date) but dont know how to pass it into date
LVL 4
bobby101Asked:
Who is Participating?
 
Infinity08Connect With a Mentor Commented:
>>  timei.tm_yday = day;

tm_yday is the day of the year (from 0 to 365).
What you want to use is the day of the month tm_mday (from 1 to 31).


You'll also have to initialize the other fields to 0, and then call mktime to fill the rest of the fields with the correct values (see the comments in the code) :
int week = 0;
char date[3] = "";
int year = 0, month = 0, day = 0;
struct tm timei = { 0 };                       // <--- initialize all fields to 0

std::cout << "dd MM yyyy : ";                  // <--- the format is without -'s
std::cin >> day >> month >> year;

timei.tm_mday = day;                           // <--- use tm_mday instead of tm_yday
timei.tm_mon = month - 1;
timei.tm_year = year - 1900;                   // <--- no += here
mktime(&timei);                                // <--- call mktime to fill the rest of the struct

strftime(date, 3, "%W", &timei);
week = atoi(date);
std::cout << week << std::endl;

Open in new window

0
 
Infinity08Commented:
strftime needs a struct tm :

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

So, you'll need to fill such a struct with the data you get from the user. Once you have that, you can pass it to strftime.
0
 
bobby101Author Commented:
i have been playing around with it but its not doing what i was please help


int week;
 char date[3];
 int year, month ,day;
 struct tm timei;
cout <<"dd-MM-yyyy";
 cin>> day >> month >> year;

 timei.tm_yday = day;
 timei.tm_mon = month-1;
 timei.tm_year += year-1900;

 strftime(date,3,"%W",&timei);
 week = atoi(date);
 cout << week;

Open in new window

0
All Courses

From novice to tech pro — start learning today.