C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

ctime

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++)
View previous topic :: View next topic  
Author Message
berkay
Guest





PostPosted: Sat Oct 29, 2005 12:24 am    Post subject: ctime Reply with quote



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

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

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

Back to top
Jonathan Mcdougall
Guest





PostPosted: Sat Oct 29, 2005 12:37 am    Post subject: Re: ctime Reply with quote



berkay wrote:

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

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

Quote:
num=ilk.getTime();//gets time

What's ilk?

Quote:
first=ctime(&num);

ctime takes a time_t*, not a long*.

Quote:
cout.flush();
fflush(stdin);

What for?

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

What's son?

Quote:
last=ctime(&num1);
cout.flush();
fflush(stdin);

2) cout<<"first:"< ///it prints the same value with last why?

Dunno.

Quote:
3) cout<<"last:"< 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


Back to top
berkay
Guest





PostPosted: Sat Oct 29, 2005 1:08 am    Post subject: Re: ctime Reply with quote



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< }
};

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<
}

void kopyala()
{
sayi=ilk.getZaman();
birinci=ctime(&sayi);
cout.flush();
fflush(stdin);

sayi1=son.getZaman();
cout<<"birinci:"< cout.flush();
fflush(stdin);


sonuncu=ctime(&sayi1);
cout<<"birinci:"< cout<<"sonuncu:"< //1 and 2 prints different values

}


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

void main(){

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




}

Back to top
Jonathan Mcdougall
Guest





PostPosted: Sat Oct 29, 2005 1:46 am    Post subject: Re: ctime Reply with quote

berkay wrote:
Quote:
here is the whole program
#include <ctime
#include #include using namespace std;



class TimeStamp{

time_t zaman;
public:

void setZaman()
{
time(&zaman);
}

time_t getZaman()
{
return zaman;
}
void print()
{

cout< }
};

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<
}

void kopyala()
{
sayi=ilk.getZaman();

A time_t is not a long!

Quote:
birinci=ctime(&sayi);
cout.flush();
fflush(stdin);

sayi1=son.getZaman();
cout<<"birinci:"< cout.flush();
fflush(stdin);


sonuncu=ctime(&sayi1);
cout<<"birinci:"< cout<<"sonuncu:"< //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 # 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;
}


Quote:
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


Back to top
Ian
Guest





PostPosted: Sat Oct 29, 2005 1:52 am    Post subject: Re: ctime Reply with quote

berkay wrote:
Quote:
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

Back to top
berkay
Guest





PostPosted: Sat Oct 29, 2005 12:11 pm    Post subject: Re: ctime Reply with quote

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

Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.