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.

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

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.

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 ;
}

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 ;
}

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.

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

This question has already been answered. Start a new discussion instead.