943,744 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 4362
  • C++ RSS
You are currently viewing page 2 of this multi-page discussion thread; Jump to the first page
Feb 25th, 2009
0

Re: Help with asctime_s and localtime_s

After making that adjustment you have to call mktime() to normalize the structure. Otherwise, I don't know what you mean, so post latest code.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,950 posts
since Aug 2005
Feb 25th, 2009
0

Re: Help with asctime_s and localtime_s

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;
	}
};
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
cppnewb is offline Offline
52 posts
since Jan 2009
Feb 25th, 2009
0

Re: Help with asctime_s and localtime_s

It's not 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)
  1. newtime.tm_mday += 14;
  2. // mktime can accept "bad" day of month
  3. time_t xtime = mktime(&newtime);
  4. // refresh newtime structure
  5. _localtime64_s(&newtime,&xtime);
Of course, you may rewrite this snippet with those funny *_s "safety" functions and long time types...
Reputation Points: 1234
Solved Threads: 347
Postaholic
ArkM is offline Offline
2,001 posts
since Jul 2008
Feb 26th, 2009
0

Re: Help with asctime_s and localtime_s

Thank you soooooooo much guys to take the time out of your day to help me with my silly questions. +1 reputation
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
cppnewb is offline Offline
52 posts
since Jan 2009

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: peincrement and postincrement operator
Next Thread in C++ Forum Timeline: #include "backward_warning.h"





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC