-
asctime woes
I am currently working on a program that will calculate the number of hours I have worked for any given day. What I have is as follows:
Code:
//////////////////////////////////////////////////////////////////////////
// calculates and outputs the number of hours worked for any given day //
//////////////////////////////////////////////////////////////////////////
#include <iostream>
#include <fstream>
#include <time.h>
time_t start, end;
struct tm * IN;
struct tm * OUT;
double diff;
void timeIN()
{
time (&start); //Get the current time
IN = localtime (&start); //Adjust to the local timezone
cout << "Time in: " << asctime (IN); //Print the time IN
}
void timeOUT()
{
time (&end); //Get the current time
OUT = localtime (&end); //Adjust to the local timezone
cout << "Time out: " << asctime (OUT); //Print the time OUT
}
void timeWORKED()
{
diff = (difftime (end, start))/3600; //Calculate the number of hours worked
cout << "Hours worked: " << diff << "\n";
}
void outPUT()
{
cout << "Outputting to timeworked.txt\n";
ofstream workFile ("timeworked.txt"); //Open timeworked.txt for output
if ( !workFile.is_open() ) { //Check to see if the file is not open
cout << "Error: Failed to open file\n"; //If the file is not open give an Error
} else { //Otherwise output the data to the file
workFile << "Time in: " << asctime (IN);
workFile << "time out: " << asctime (OUT);
workFile << "Hours worked: " << diff << "\n";
workFile.close();
}
}
void error()
{
cout << "Error!!!\n";
}
void menu()
{
int opt; //Option to switch
cout << "[1] Enter your time IN \n";
cout << "[2] Enter your time OUT \n";
cout << "[3] Calculate your total time Worked\n";
cout << "[4] Output to a file\n";
cout << "[5] Exit\n";
cout << "Enter your choice: ";
cin >> opt; //Get the otption to switch
switch ( opt ) { // switch the option
case 1:
timeIN();
break;
case 2:
timeOUT();
break;
case 3:
timeWORKED();
break;
case 4:
outPUT();
break;
case 5:
exit(0);
default:
error();
break;
}
}
int main()
{
while (1) {
menu();
}
return 0;
}
At a quick glance the program gives off the illusion that it is working, however, it is not. The functions void timeIN() (ln 14) and void timeOUT() (ln 21) print two unique times and dates by way of asctime(IN) (ln 18) and asctime(OUT) (ln 25), as they should. Sadly, in my function void outPUT() (ln 34), the output given by asctime(IN) (ln 41) and asctime(OUT) (42) is not identical to the output given by the same calls in past lines. What could be causing this?
-
This worked perfectly fine for me:
Code:
#include <iostream>
#include <fstream>
#include <ctime>
using namespace std;
time_t start, end;
struct tm *IN, *OUT;
double diff;
void timeIN() {
time (&start); //Get the current time
IN = localtime (&start); //Adjust to the local timezone
cout << "Time in: " << asctime (IN); //Print the time IN
}
void timeOUT() {
time (&end); //Get the current time
OUT = localtime (&end); //Adjust to the local timezone
cout << "Time out: " << asctime (OUT); //Print the time OUT
}
void timeWORKED() {
diff = (difftime (end, start))/3600; //Calculate the number of hours worked
cout << "Hours worked: " << diff << "\n";
}
void outPUT() {
cout << "Outputting to timeworked.txt\n";
ofstream workFile ("timeworked.txt"); //Open timeworked.txt for output
if ( !workFile.is_open() ) { //Check to see if the file is not open
cout << "Error: Failed to open file\n"; //If the file is not open give an Error
} //Otherwise output the data to the file
else {
workFile << "Time in: " << asctime (IN);
workFile << "time out: " << asctime (OUT);
workFile << "Hours worked: " << diff << "\n";
workFile.close();
}
}
void error() {
cout << "Error!!!\n";
}
void menu() {
int opt;
cout << "[1] Enter your time IN \n";
cout << "[2] Enter your time OUT \n";
cout << "[3] Calculate your total time Worked\n";
cout << "[4] Output to a file\n";
cout << "[5] Exit\n";
cout << "Enter your choice: ";
cin >> opt;//you need error checking and recover here!
switch ( opt ) {
case 1: timeIN(); break;
case 2: timeOUT(); break;
case 3: timeWORKED(); break;
case 4: outPUT(); break;
case 5: exit(0);
default: error();
}
}
int main() {
while (1) {
menu();
}
return 0;
}
-
If you open the file timeworked.txt you get this:
Code:
Time in: Thu Jun 23 12:41:24 2005
time out: Thu Jun 23 12:41:24 2005
Hours worked: 0.000277778
However, the original data printed by asctime(IN) is:
Code:
Time in: Thu Jun 23 12:41:23 2005
While the data printed by asctime(OUT) is:
Code:
Time out: Thu Jun 23 12:41:24 2005
-
> struct tm * IN;
> struct tm * OUT;
You can't have two localtime's on the go at the same time. If you print this pointer, you should find them both pointing at the same block of memory.
Try these bits, with other appropriate adjustments to your code.
struct tm IN;
struct tm OUT;
IN = *localtime (&start);
-
Read up on the documentation; I believe localtime may return a pointer to a local buffer, in which case IN and OUT would point to exactly the same memory location.
-
Mission accomplished :D . But I am a little confused as to why a reference would work and not a pointer, would someone please explain this? To be honest... I just threw the reference operator in there because I remember reading from my book that references and pointers are almost exactly the same.
And for those who have the same problem, this is the finished product:
Code:
//////////////////////////////////////////////////////////////////////////
// calculates and outputs the number of hours worked for any given day //
//////////////////////////////////////////////////////////////////////////
#include <iostream>
#include <fstream>
#include <time.h>
time_t start, end;
struct tm IN;
struct tm OUT;
double diff;
void timeIN()
{
time (&start); //Get the current time
IN = *localtime (&start); //Adjust to the local timezone
cout << "Time in: " << asctime (&IN); //Print the time IN
}
void timeOUT()
{
time (&end); //Get the current time
OUT = *localtime (&end); //Adjust to the local timezone
cout << "Time out: " << asctime (&OUT); //Print the time OUT
}
void timeWORKED()
{
diff = (difftime (end, start))/3600; //Calculate the number of hours worked
cout << "Hours worked: " << diff << "\n";
}
void outPUT()
{
cout << "Outputting to timeworked.txt\n";
ofstream workFile ("timeworked.txt"); //Open timeworked.txt for output
if ( !workFile.is_open() ) { //Check to see if the file is not open
cout << "Error: Failed to open file\n"; //If the file is not open give an Error
} else { //Otherwise output the data to the file
workFile << "Time in: " << asctime (&IN);
workFile << "time out: " << asctime (&OUT);
workFile << "Hours worked: " << diff << "\n";
workFile.close();
}
}
void error()
{
cout << "Error!!!\n";
}
void menu()
{
int opt; //Option to switch
cout << "[1] Enter your time IN \n";
cout << "[2] Enter your time OUT \n";
cout << "[3] Calculate your total time Worked\n";
cout << "[4] Output to a file\n";
cout << "[5] Exit\n";
cout << "Enter your choice: ";
cin >> opt; //Get the otption to switch
switch ( opt ) { // switch the option
case 1:
timeIN();
break;
case 2:
timeOUT();
break;
case 3:
timeWORKED();
break;
case 4:
outPUT();
break;
case 5:
exit(0);
default:
error();
break;
}
}
int main()
{
while (1) {
menu();
}
return 0;
}
-
You are putting the DE-reference operator, which then sets up for a copy to the NON-pointer tm struct. Thus you are making a copy of the actual data instead of a copy of the pointer. Day and night difference!
-
That makes sense, thanks.