| | |
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: peincrement and postincrement operator
- Next Thread: #include "backward_warning.h"
Views: 1569 | Replies: 13
| Thread Tools | Search this Thread |
Tag cloud for C++
6 api application array arrays assignment based beginner binary c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count data database delete developer display dll dynamiccharacterarray email encryption error file format forms fstream function functions game generator givemetehcodez graph iamthwee ifstream image input int java lib list loop looping loops map math matrix memory multidimensional multiple newbie news number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg search sort sorting string strings struct template templates text tree url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets






