Bug in getting the current date

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Dec 2006
Posts: 19
Reputation: jobra is an unknown quantity at this point 
Solved Threads: 0
jobra jobra is offline Offline
Newbie Poster

Bug in getting the current date

 
0
  #1
Dec 13th, 2006
Hi

I am new to c++. I tried to get current date but when I compile the program, it gives me an error. This is my program
  1. #include <iostream>
  2. #include <ctime>
  3. #include <stdio.h>
  4. int main ()
  5. {
  6. char sdate [10];
  7. _strdate(sdate);
  8. std::cout<<"Current Date:"<<sdate;
  9. return 0;
  10. }
when I complie in cygwin, the error is
test.cpp:8: error: รข was not declared in this scope


Where are am I making a mistake. Please help me. Its urgent. Thanks a lot in advance.
Last edited by ~s.o.s~; Dec 13th, 2006 at 10:22 pm. Reason: Added code tags learn to use them yourself.
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 1,580
Reputation: Infarction has a spectacular aura about Infarction has a spectacular aura about Infarction has a spectacular aura about 
Solved Threads: 52
Infarction's Avatar
Infarction Infarction is offline Offline
Battle Programmer

Re: Bug in getting the current date

 
0
  #2
Dec 13th, 2006
1) Where is _strdate() defined? I'm not finding it...
2) Why are you using <iostream> and <stdio.h> (which should be <cstdio>)?
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 5,051
Reputation: John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold 
Solved Threads: 332
Team Colleague
John A's Avatar
John A John A is offline Offline
Vampirical Lurker

Re: Bug in getting the current date

 
1
  #3
Dec 13th, 2006
You need to include time.h to be able to use _strdate() - but please note that this is a non-standard function (doesn't work in Unix).

And please use code tags.
Last edited by John A; Dec 13th, 2006 at 10:19 pm.
"Technological progress is like an axe in the hands of a pathological criminal."

All my posts may be freely redistributed under the terms of the MIT license.
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 7,617
Reputation: ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of 
Solved Threads: 466
Super Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Failure as a human

Re: Bug in getting the current date

 
0
  #4
Dec 13th, 2006
Here is the reference site.

Here is a small example:
  1. #include <iostream>
  2. #include <ctime>
  3. using namespace std ;
  4.  
  5. int main( )
  6. {
  7. char buffer[BUFSIZ] = { '\0' } ;
  8. time_t now = time( &now ) ;
  9. struct tm* local_time = new tm( ) ;
  10. local_time = localtime( &now ) ;
  11.  
  12. strftime( buffer, BUFSIZ, "%d / %m / %Y", local_time ) ;
  13. cout << buffer ;
  14. }
I don't accept change; I don't deserve to live.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,408
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1469
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Bug in getting the current date

 
0
  #5
Dec 14th, 2006
or this, which is almost identical to what ~S.O.S.~ posted. Like most things in programming there is always more than one way to do it. Unless you are instructed to do otherwise you can choose whichever method you want. One is not any better than the other unless you make the buffer too small to hold the result. In that case neither version will work correctly.

  1. #include <iostream>
  2. #include <ctime>
  3. using namespace std ;
  4.  
  5. int main( )
  6. {
  7. char buffer[BUFSIZ] = { '\0' } ;
  8. time_t now = time( &now ) ;
  9. struct tm* local_time = new tm( ) ;
  10. local_time = localtime( &now ) ;
  11. sprintf(buffer,"%02d/%02d/%04d",
  12. local_time->tm_mday,
  13. local_time->tm_mon+1,
  14. local_time->tm->year+1900);
  15. cout << buffer ;
  16. }
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,362
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 241
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: Bug in getting the current date

 
0
  #6
Dec 14th, 2006
Isn't this leaking memory (aside from the fact that no attempt is made to delete the lost memory)?
    struct tm* local_time = new tm( ) ;
    local_time = localtime( &now ) ;
It is also a good habit to check return values.
Time - Displaying Current
Change the format fed to strftime, as already mentioned, for the date.
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,408
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1469
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Bug in getting the current date

 
0
  #7
Dec 14th, 2006
Originally Posted by Dave Sinkula View Post
Isn't this leaking memory (aside from the fact that no attempt is made to delete the lost memory)?
yes it is -- I hadn't noticed that error.:o
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 19
Reputation: jobra is an unknown quantity at this point 
Solved Threads: 0
jobra jobra is offline Offline
Newbie Poster

Re: Bug in getting the current date

 
0
  #8
Dec 14th, 2006
Hi everyone,

Thanxs a lot. It worked. Cheers
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC