| | |
Help with asctime_s and localtime_s
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
Ok then... Here is the latest code (with the thing that causes the runtime error):
class renewBooks
{
public:
int getItems()
{
cout << "Please scan the barcode of the book you would like to renew:\n";
cin >> renewBarcode;
processItems(renewBarcode);
return false;
}
private:
string renewBarcode;
string toRenewItem;
#include <time.h>
#include <stdio.h>
#include <string.h>
int processItems(string toRenewItem)
{
struct tm newtime;
char am_pm[] = "AM";
time_t long_time;
char timebuff [26];
errno_t err;
//Get time as a 64 bit integer
time( &long_time );
// Convert to local time
err = _localtime64_s( &newtime, &long_time );
if (err)
{
printf( "Invalid argument to _local time64_s.");
exit(1);
}
if(newtime.tm_hour > 12)
strcpy_s(am_pm, sizeof(am_pm), "PM");
if (newtime.tm_hour > 12)
newtime.tm_hour -= 12;
if (newtime.tm_hour == 0)
newtime.tm_hour = 12;
// This is where the error is:[
newtime.tm_mday += 14;
// ]This is where the error ends
// Convert to an ASCII representation
err = asctime_s(timebuff, 26, &newtime);
if (err)
{
printf ( "Invalid argument to asctime_s.");
exit(1);
}
ofstream renew ("renewBooks.txt", std::ios::app);
renew << "Date: "
<< "\nTime: "
<< "\nDue date: ";
renew printf( "%.19s %s\n", timebuff, am_pm );
renew << "\nBook barcode: "
<< toRenewItem;
renew.close();
return false;
}
}; It's not
To normalize "moved forward" newtime structure add some conversions:
Of course, you may rewrite this snippet with those funny *_s "safety" functions and long time types...
newtime.tm_mday += 14; statement failed: the next one. The asctime_s assertion failed when the function processes tm_mday == 40. Obviously, this future day of month (26+14) is invalid.To normalize "moved forward" newtime structure add some conversions:
c++ Syntax (Toggle Plain Text)
newtime.tm_mday += 14; // mktime can accept "bad" day of month time_t xtime = mktime(&newtime); // refresh newtime structure _localtime64_s(&newtime,&xtime);
![]() |
Similar Threads
Other Threads in the C++ Forum
- Previous Thread: POSIX Message Queue
- Next Thread: #include "backward_warning.h"
| Thread Tools | Search this Thread |
api array arrays based beginner binary bitmap c++ c/c++ calculator char char* class classes coding compile compiler console conversion count data database delete deploy desktop developer directshow dll dynamiccharacterarray email encryption error file forms fstream function functions game getline google graph homeworkhelp homeworkhelper iamthwee ifstream input int integer lib linkedlist linux list loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference return rpg sorting string strings struct template templates text tree unix url vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






