mktime() code example fixed

/* mktime example: weekday calculator */
/* fixed by P C */

#include <stdio.h>
#include <time.h>

int main(){
time_t rawtime;
struct tm *timeinfo;
int year, month ,day;
char buffer[64]="";

/* prompt user for date */
printf("\\nEnter year (number): "); scanf("%d",&year);
printf("\\nEnter month (number): "); scanf("%d",&month);
printf("\\nEnter day (number): "); scanf("%d",&day);

/* get current timeinfo and modify it to the user's choice */
time(&rawtime);
timeinfo = localtime(&rawtime);
timeinfo->tm_year = year - 1900;
timeinfo->tm_mon = month - 1;
timeinfo->tm_mday = day;

/* call mktime: timeinfo->tm_wday will be set */
mktime(timeinfo);

/* display the weekday (this is a fix for the deprecated conversion to char*) */
strftime(buffer,64,"That day is a %A",timeinfo);

printf("\n%s\n", buffer);

return 0;
}
Last edited on
I really don't understand what you are trying to say.
just a lump of code, without tag, in the lounge ?
Ok, would you like us to review your code? Are you looking for constructive feedback? Or just a pat on the back and some kind of boost to your ego?
I believe the OP attempted to correct the sample code shown on the reference page of this website: http://cplusplus.com/reference/clibrary/ctime/mktime/ which appears to have been written as a C89 program, and, among other issues, attempts to initialize char* pointers using string literals.

strftime (or its C++11 equivalent, put_time) is indeed a very reasonable way to approach this, see for example the rival website's example: http://en.cppreference.com/w/cpp/chrono/c/mktime
Last edited on
Topic archived. No new replies allowed.