User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C++ section within the Software Development category of DaniWeb, a massive community of 427,680 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 4,298 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C++ advertiser: Programming Forums
Views: 2064 | Replies: 7 | Solved
Reply
Join Date: Dec 2006
Posts: 19
Reputation: jobra is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
jobra jobra is offline Offline
Newbie Poster

Bug in getting the current date

  #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 9:22 pm. Reason: Added code tags learn to use them yourself.
AddThis Social Bookmark Button
Reply With Quote  
Join Date: May 2006
Location: Bellevue, WA
Posts: 1,548
Reputation: Infarction has a spectacular aura about Infarction has a spectacular aura about Infarction has a spectacular aura about 
Rep Power: 8
Solved Threads: 51
Sponsor
Infarction's Avatar
Infarction Infarction is offline Offline
Battle Programmer

Re: Bug in getting the current date

  #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  
Join Date: Apr 2006
Location: Canada
Posts: 4,505
Reputation: John A is a glorious beacon of light John A is a glorious beacon of light John A is a glorious beacon of light John A is a glorious beacon of light John A is a glorious beacon of light John A is a glorious beacon of light 
Rep Power: 17
Solved Threads: 276
Moderator
Featured Blogger
John A's Avatar
John A John A is offline Offline
Vampirical Moderator

Re: Bug in getting the current date

  #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 9:19 pm.
tuxation.com - Linux articles, tutorials, and discussions
Reply With Quote  
Join Date: Jun 2006
Location: India
Posts: 6,863
Reputation: ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold 
Rep Power: 23
Solved Threads: 344
Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Lazy, Useless & Apathetic

Re: Bug in getting the current date

  #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.

Happiness corrupts people.

Failing to value the lives of others cheapens your own.
Reply With Quote  
Join Date: Aug 2005
Location: near St Louis, Missouri, USA
Posts: 11,222
Reputation: Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of 
Rep Power: 38
Solved Threads: 934
Moderator
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Most Valuable Poster

Re: Bug in getting the current date

  #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.

#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 ;
}
I think it's about time we voted for senators with breasts. After all, we've been voting for boobs long enough. ~Clarie Sargent, Arizona senatorial candidate
Those who are too smart to engage in politics are punished by being governed by those who are dumber. ~Plato
Reply With Quote  
Join Date: Apr 2004
Posts: 3,648
Reputation: Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light 
Rep Power: 17
Solved Threads: 144
Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: Bug in getting the current date

  #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.
Reply With Quote  
Join Date: Aug 2005
Location: near St Louis, Missouri, USA
Posts: 11,222
Reputation: Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of 
Rep Power: 38
Solved Threads: 934
Moderator
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Most Valuable Poster

Re: Bug in getting the current date

  #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
I think it's about time we voted for senators with breasts. After all, we've been voting for boobs long enough. ~Clarie Sargent, Arizona senatorial candidate
Those who are too smart to engage in politics are punished by being governed by those who are dumber. ~Plato
Reply With Quote  
Join Date: Dec 2006
Posts: 19
Reputation: jobra is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
jobra jobra is offline Offline
Newbie Poster

Re: Bug in getting the current date

  #8  
Dec 14th, 2006
Hi everyone,

Thanxs a lot. It worked. Cheers
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb C++ Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the C++ Forum

All times are GMT -4. The time now is 11:16 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC