How can I creat a time function or a class?

L

learning_C++

I programmed this code with a function "get_current_time" in the
begining. When I compiled with the command g++ -Wall -g xxx.xpp -o xxx
there are so many errors.
please help me and thanks,

#include <map>
#include <iostream>
#include <iomanip>
#include <string>
#include <time.h>

//using namespace std;


string get_current_time(){
string timestr;
time_t rawtime;
struct tm * timeinfo;
time_t time ( &rawtime );
timeinfo = localtime ( &rawtime );
timestr=asctime(timeinfo);
}
bank.cpp:14: error: invalid conversion from `time_t*' to `int'
bank.cpp:15: error: ISO C++ forbids declaration of `timeinfo' with no
type
bank.cpp:15: error: conflicting types for `int timeinfo'
bank.cpp:13: error: previous declaration as `tm*timeinfo'
bank.cpp:16: error: ISO C++ forbids declaration of `timestr' with no
type
bank.cpp:16: error: invalid conversion from `int' to `const tm*'
bank.cpp:16: error: invalid conversion from `char*' to `int'
bank.cpp:17: error: parse error before `}' token
 
J

John Harrison

learning_C++ said:
I programmed this code with a function "get_current_time" in the
begining. When I comp iledwiththecommandg++-Wall-gxxx.xpp-oxxx
there are so many errors.
please help me and thanks,

#include <map>
#include <iostream>
#include <iomanip>
#include <string>
#include <time.h>

//using namespace std;


string get_current_time(){
string timestr;
time_t rawtime;
struct tm * timeinfo;
time_t time ( &rawtime );

time(&rawtime);

Because you put 'time_t time(&rawtime);' the compiler thinks you are trying
to declare a variable called time.
timeinfo = localtime ( &rawtime );
timestr=asctime(timeinfo);
}

john
 
J

John Harrison

John Harrison said:
time(&rawtime);

Because you put 'time_t time(&rawtime);' the compiler thinks you are
trying to declare a variable called time.

Also

return timestr;

If you want to return a value from a function you must say return. It
doesn't happen by magic.

john
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,577
Members
45,054
Latest member
LucyCarper

Latest Threads

Top