ctime

Discussion in 'C++' started by berkay, Oct 29, 2005.

  1. berkay

    berkay Guest

    long num,num1;
    char *first;
    char *last;

    num=ilk.getTime();//gets time
    first=ctime(&num);
    cout.flush();
    fflush(stdin);
    1) cout<<"first:"<<first;

    num1=son.getTime();
    last=ctime(&num1);
    cout.flush();
    fflush(stdin);

    2)cout<<"first:"<<first<<endl;///it prints the same value with last
    why?
    3) cout<<"last:"<<last<<endl;
    1 and 2 prints different values,2 and 3 prints the same value why?
     
    berkay, Oct 29, 2005
    #1
    1. Advertising

  2. berkay wrote:

    > long num,num1;
    > char *first;
    > char *last;


    This is bad style in C++. You should always initialize your variables.

    > num=ilk.getTime();//gets time


    What's ilk?

    > first=ctime(&num);


    ctime takes a time_t*, not a long*.

    > cout.flush();
    > fflush(stdin);


    What for?

    > 1) cout<<"first:"<<first;
    > num1=son.getTime();


    What's son?

    > last=ctime(&num1);
    > cout.flush();
    > fflush(stdin);
    >
    > 2) cout<<"first:"<<first<<endl;
    > ///it prints the same value with last why?


    Dunno.

    > 3) cout<<"last:"<<last<<endl;
    > 1 and 2 prints different values,2 and 3 prints the same value why?


    Again, how do you expect us to answer (or understand) that?

    Please post complete, compilable, well-formated programs along with a
    clear question.


    Jonathan
     
    Jonathan Mcdougall, Oct 29, 2005
    #2
    1. Advertising

  3. berkay

    berkay Guest

    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())<<endl;

    }

    void kopyala()
    {
    sayi=ilk.getZaman();
    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

    }


    void ctimeOlarak()
    {
    ilk.print();
    son.print();
    }
    };

    void main(){

    Task myTask;
    myTask.zamanyazdir();
    myTask.kopyala();
    myTask.zamanFarki();
    myTask.ctimeOlarak();




    }
     
    berkay, Oct 29, 2005
    #3
  4. 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())<<endl;
    >
    > }
    >
    > 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
     
    Jonathan Mcdougall, Oct 29, 2005
    #4
  5. berkay

    Ian Guest

    berkay wrote:
    > ilk.setZaman();
    > for(int i=0;i<1000000000;i++){}//for the times to be different


    Don't do this, use an appropriate delay function.

    Ian
     
    Ian, Oct 29, 2005
    #5
  6. berkay

    berkay Guest

    yes s1 and s2 works well but if i want to make such a thing
    strstreambuf bbuf(char*,int);
    it only accepts a pointer and i have a string how can i do this i ll be
    happy if u can help me
     
    berkay, Oct 29, 2005
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ernst Murnleitner

    replacement for <ctime>?

    Ernst Murnleitner, Jul 16, 2003, in forum: C++
    Replies:
    8
    Views:
    542
    Limech
    Jul 20, 2003
  2. Tivo Escobar

    Question about ctime

    Tivo Escobar, Jul 23, 2003, in forum: C++
    Replies:
    1
    Views:
    401
    Victor Bazarov
    Jul 23, 2003
  3. Oplec
    Replies:
    2
    Views:
    529
    Oplec
    Oct 26, 2003
  4. Kevin

    ctime Help

    Kevin, May 16, 2004, in forum: C++
    Replies:
    3
    Views:
    798
    osmium
    May 16, 2004
  5. wukexin
    Replies:
    1
    Views:
    478
    Karl Heinz Buchegger
    May 25, 2005
Loading...

Share This Page