| | |
Bug in getting the current date
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
•
•
Join Date: Dec 2006
Posts: 19
Reputation:
Solved Threads: 0
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
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.
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
C++ Syntax (Toggle Plain Text)
#include <iostream> #include <ctime> #include <stdio.h> int main () { char sdate [10]; _strdate(sdate); std::cout<<"Current Date:"<<sdate; return 0; }
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.
You need to include
And please use code tags.
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.
All my posts may be freely redistributed under the terms of the MIT license.
Here is the reference site.
Here is a small example:
Here is a small example:
C++ Syntax (Toggle Plain Text)
#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 ; }
I don't accept change; I don't deserve to live.
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.
C++ Syntax (Toggle Plain Text)
#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 ; }
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.
Isn't this leaking memory (aside from the fact that no attempt is made to delete the lost memory)? It is also a good habit to check return values.
Time - Displaying Current
Change the format fed to
struct tm* local_time = new tm( ) ; local_time = localtime( &now ) ;
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
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.
![]() |
Similar Threads
- How to get the current date in JAVA (JSP)
- Passing data from forms from one page to another (JavaScript / DHTML / AJAX)
- How to Add date to file Name (Windows NT / 2000 / XP)
- How to Get Last Accessed/Created/Modified File Date and Time (C)
- ASP Date Help - If Then Else (ASP)
- Actual Date to Day of the Week Application (Java)
- gwt the record which match the current date (Visual Basic 4 / 5 / 6)
Other Threads in the C++ Forum
- Previous Thread: help with dynamically allocated arrays
- Next Thread: quick questions
| Thread Tools | Search this Thread |
api array arrays based beginner binary bitmap c++ c/c++ calculator char class classes code compile compiler console conversion count data delete deploy desktop developer directshow dll download dynamic encryption error file forms fstream function functions game getline givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory news node number output parameter pointer problem program programming project proxy python read recursion recursive return string strings struct temperature template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






