Need help with strftime...

hi, i'm trying to output the week of the year but everytime i enter a date i get 2...
what am i doing wrong...
#include<iostream>
  #include <stdlib.h>	// for itoa() call
#include <stdio.h>	// for printf() call
#include<time.h>
using namespace std;
int main ()
{   

	struct tm tm;
	int cruiseNum,day,month,year;
  	char tmbuf[64];
	
	memset(&tm,0,sizeof(struct tm));
	
	tm.tm_sec=0;
	tm.tm_min=0;
	tm.tm_hour=23;
	tm.tm_mday=day;
	tm.tm_mon=month;
	tm.tm_year=year;
	
		cout<<"enter day";
	cin>> day;
	cout<<"enter month";
	cin>>month;
	cout<<"enter year";
	cin>>year;
	
	mktime(&tm);
	cruiseNum=strftime(tmbuf,sizeof(tmbuf),"%W",&tm);

	
  system("pause");
  return 0;
}

Open in new window

FallenAngel1Asked:
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.

gplanaCommented:

You are setting tm fields after asking values to user. You should set the values when initialized by user:
#include<iostream>
  #include <stdlib.h>	// for itoa() call
#include <stdio.h>	// for printf() call
#include<time.h>
using namespace std;
int main ()
{   

	struct tm tm;
	int cruiseNum,day,month,year;
  	char tmbuf[64];
	
	memset(&tm,0,sizeof(struct tm));
	

	
		cout<<"enter day";
	cin>> day;
	cout<<"enter month";
	cin>>month;
	cout<<"enter year";
	cin>>year;

	tm.tm_sec=0;
	tm.tm_min=0;
	tm.tm_hour=23;
	tm.tm_mday=day;
	tm.tm_mon=month;
	tm.tm_year=year;

	mktime(&tm);
	cruiseNum=strftime(tmbuf,sizeof(tmbuf),"%W",&tm);

	
  system("pause");
  return 0;
}

Open in new window

0
FallenAngel1Author Commented:
i still get 2 no matter what date i enter
0
FallenAngel1Author Commented:
am i doing something else wrong?
0
Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

phoffricCommented:
See mktime
    http://www.cplusplus.com/reference/clibrary/ctime/mktime/
Notice code from example shown below.

Here is the sample input from this reference:

Enter year: 2000
Enter month: 5
Enter day: 20

You have to be careful with month (starts with 0) and year (offset from 1900).


Also, note that according to strftime reference,
    http://www.cplusplus.com/reference/clibrary/ctime/strftime/
the W numbering starts at 0:
     "%W Week number with the first Monday as the first day of week one (00-53) "
  timeinfo->tm_year = year - 1900;
  timeinfo->tm_mon = month - 1;

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
phoffricCommented:
Also note what the return value from strftime says:
  "... the total number of characters copied to ptr (not including the terminating null-character) is returned."

You are writing two chars into your tmpbuf, so strftime returns a 2.
0
FallenAngel1Author Commented:
how do i fix dat...i've never used strftime before this is my first time..
0
phoffricCommented:
if year is 2010, subtract 1900 to get 110 --> year
if month is jan, then set month to 0; if feb, set month to 1, etc.

then tmpbuf has a string that is the week in the year
Use atoi to convert the string to an integer.
     http://www.cplusplus.com/reference/clibrary/cstdlib/atoi/

code is from the example in the link.

int i;
i= atoi (szInput);

Open in new window

0
phoffricCommented:
But the number of the week starts with 0. So, adjust according to your needs.
0
FallenAngel1Author Commented:
>> if month is jan, then set month to 0; if feb, set month to 1, etc.
but i'm asking user to input the month... so shouldn't i set it to month variable.
0
phoffricCommented:
If you got a week number of 0, that must mean that the monday of that week fell in the previous year. So if you choose jan 1,2010, then you get a 0 because jan 1 is friday and it belongs to the week beginning on the last monday in december, 2009.

If you enter dec 31, 2012, then that is week 53 (check the calendar)
    http://www.timeanddate.com/calendar/?year=2012&country=1

0
phoffricCommented:
>> i'm asking user to input the month
if user enters 1 for jan, then you will need to subtract 1 to get a 0 since that is what struct tm defines month to be.
0
FallenAngel1Author Commented:
thank you for ur help... got it working..

0
phoffricCommented:
ur very welcome. good luck in your future studies.
0
FallenAngel1Author Commented:
thank you again
0
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.