The program below is loosely based on some of the examples in the reference section of this site. It should take in two dates (currently they are defined directly in the program for easier debugging) and output the day of the week of the first one. Instead, it outputs the day of the week of the second one. Any idea why?
helios - I know, but as I understand it mktime will do that that, at least for tm_wday. This program works just fine when only one date is involved, but the second one seems to be interfering with the first.
IIRC, localtime() returns a pointer to a shared structure, so date1 and date2 point to the same thing.
Wht you could do is make them non-pointers and then date=*localtime();
#include <time.h> // library for storing and performing simple functions on dates using time_t and struct tm
#include <iostream>
usingnamespace std;
int main()
{
// vars
time_t currt1, currt2; // store current time
struct tm date1, date2; // store the two dates
int y1, y2, m1, m2, d1, d2; // store user input
// get user input
cout << "Please enter the first date (m d yyyy):" << endl;
//cin >> m1 >> d1 >> y1;
cout << "Please enter the second date (m d yyyy):" << endl;
//cin >> m2 >> d2 >> y2;
m1 = 5;
d1 = 20;
y1 = 2000;
m2 = 6;
d2 = 21;
y2 = 2001;
// put user input into date1, date2
time(&currt1);
date1 = *localtime(&currt1);
date1.tm_year = y1 - 1900;
date1.tm_mon = m1 - 1;
date1.tm_mday = d1;
time(&currt1);
date2 = *localtime(&currt1);
date2.tm_year = y2 - 1900;
date2.tm_mon = m2 - 1;
date2.tm_mday = d2;
// fill in the blanks in date1, date2
mktime(&date1);
mktime(&date2);
// output
cout << date1.tm_wday << endl;
return 0;
}