[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 326
  • Last Modified:

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

0
FallenAngel1
Asked:
FallenAngel1
  • 7
  • 6
2 Solutions
 
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
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

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

Featured Post

Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

  • 7
  • 6
Tackle projects and never again get stuck behind a technical roadblock.
Join Now