berkay wrote:
> here is the whole program
> #include <ctime>
> #include<iostream>
> #include<strstream>
> using namespace std;
>
>
>
> class TimeStamp{
>
> time_t zaman;
> public:
>
> void setZaman()
> {
> time(&zaman);
> }
>
> time_t getZaman()
> {
> return zaman;
> }
> void print()
> {
>
> cout<<ctime(&zaman)<<endl;
> }
> };
>
> class Task{
> private:
> TimeStamp ilk;
> TimeStamp son;
> char *birinci;
> char *sonuncu;
> long sayi,sayi1;
> char totalday[24];
> char gun[4];
> char ay[4];
> int ayinKaci,hour,min,sec,yil;
> char arr[3];
> char yilim[5];
> public:
> void zamanyazdir()
> {
> ilk.setZaman();
> for(int i=0;i<1000000000;i++){}//for the times to be different
> son.setZaman();
> }
>
> void zamanFarki()
> {
>
> cout<<difftime(son.getZaman(),ilk.getZaman())<<end l;
>
> }
>
> void kopyala()
> {
> sayi=ilk.getZaman();
A time_t is not a long!
> birinci=ctime(&sayi);
> cout.flush();
> fflush(stdin);
>
> sayi1=son.getZaman();
> cout<<"birinci:"<<birinci<<endl;//1
> cout.flush();
> fflush(stdin);
>
>
> sonuncu=ctime(&sayi1);
> cout<<"birinci:"<<birinci<<endl;//2
> cout<<"sonuncu:"<<sonuncu<<endl;//3
> //1 and 2 prints different values
Yes. ctime() returns a static pointer to a char. Every time you call
it, it will modify it. You should copy that string as soon as you get
it:
# include <string>
# include <ctime>
# include <iostream>
int main()
{
std::time_t now = std::time(0);
// make a copy in 's'
std::string s = std::ctime(&now);
std::cout << "s:" << s << std::endl;
// wait a couple of seconds
now = std::time(0);
std::string s2 = std::ctime(&now);
std::cout << "s:" << s << std::endl
<< "s2:" << s2;
}
<snip>
> void main(){
main() returns an int.
Note that this is an english-only newsgroup. It will be easier for
everybody if you translate the names in english.
Jonathan
|