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?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
Infinity08Commented:
>>  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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Editors IDEs

From novice to tech pro — start learning today.