473,385 Members | 1,942 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

ctime

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?

Oct 29 '05 #1
5 4639
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

Oct 29 '05 #2
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();
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();


}

Oct 29 '05 #3
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

Oct 29 '05 #4
Ian
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
Oct 29 '05 #5
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

Oct 29 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Kevin | last post by:
Hello: I am trying to find documentation on using the ctime library as I need to be able to instantiate an object to keep track of the time when a certain event occurs. I also need to be able...
1
by: wukexin | last post by:
I write my own class Cfile, I want to know what about implement ctime().Who help me? My use function ctime, I sign it with $$$. my class Cfile: #------------------------ file.h...
2
by: Hao Xu | last post by:
Hi everyone! I think that everyone knows ctime() in glibc: The ctime(), gmtime() and localtime() functions all take an argument of data type time_t which represents calendar time. When ...
2
by: Chris | last post by:
Hello, How can i convert a C++ CTime (4 bytes) into a C# DateTime ? (my CTime is read in a file). Thanks, -- Chris
4
by: Gary Wessle | last post by:
Hi I am not getting current time with this program, what am I doing wrong? #include <ctime> #include <iostream> using namespace std; #define P(x) cout << #x " = " << (x) << "\n";
11
by: aisling.cronin | last post by:
Hi I am using ctime to convert the following string 1144412677847 .... Please could some one to double check if they get the same result as me (The Time is Sun Nov 02 09:11:51 2031). It seems...
4
by: Pietro Cerutti | last post by:
Hi group, #include <stdio.h> #include <unistd.h> #include <time.h> int main(void) { time_t t1, t2; char *st1, *st2;
1
by: parag_paul | last post by:
I am try ing to use ctime with the type time_t , I used .... time_t t = time(0); char* sc = ctime( &t); printf ("%s\n", sc); ....
9
by: Lars Uffmann | last post by:
Is the ctime library really that poorly designed, or is it just me looking to do things the wrong way? The whole concept of the struct tm seems way too highlevel for my taste - this is something...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.