954,506 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Bug in getting the current date

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

#include <iostream>
#include <ctime>
#include <stdio.h>
int main ()
{
  char sdate [10];
  _strdate(sdate);
  std::cout<<"Current Date:"<<sdate;
  return 0;
}

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.

jobra
Newbie Poster
19 posts since Dec 2006
Reputation Points: 10
Solved Threads: 0
 

1) Where is _strdate() defined? I'm not finding it...
2) Why are you using and (which should be )?

Infarction
Posting Virtuoso
1,580 posts since May 2006
Reputation Points: 683
Solved Threads: 53
 

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.

John A
Vampirical Lurker
Team Colleague
7,630 posts since Apr 2006
Reputation Points: 2,240
Solved Threads: 339
 

Here is the reference site .

Here is a small example:

#include <iostream>
#include <ctime>
using namespace std ;

int main( )
{
    char buffer[BUFSIZ] = { '\0' } ;
    time_t now = time( &now ) ;
    struct tm* local_time = new tm( ) ;
    local_time = localtime( &now ) ;

    strftime( buffer, BUFSIZ, "%d / %m / %Y", local_time ) ;
    cout << buffer ;
}
~s.o.s~
Failure as a human
Administrator
11,938 posts since Jun 2006
Reputation Points: 3,281
Solved Threads: 734
 

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.

#include <iostream>
#include <ctime>
using namespace std ;
 
int main( )
{
    char buffer[BUFSIZ] = { '\0' } ;
    time_t now = time( &now ) ;
    struct tm* local_time = new tm( ) ;
    local_time = localtime( &now ) ;
    sprintf(buffer,"%02d/%02d/%04d",
          local_time->tm_mday,
          local_time->tm_mon+1,
          local_time->tm->year+1900); 
    cout << buffer ;
}
Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

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.

Dave Sinkula
long time no c
Team Colleague
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
 
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

Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

Hi everyone,

Thanxs a lot. It worked. Cheers

jobra
Newbie Poster
19 posts since Dec 2006
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You