473,802 Members | 2,431 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

ctime

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

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

num1=son.getTim e();
last=ctime(&num 1);
cout.flush();
fflush(stdin);

2)cout<<"first: "<<first<<e ndl;///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 4661
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(&nu m);
ctime takes a time_t*, not a long*.
cout.flush();
fflush(stdin);
What for?
1) cout<<"first:"< <first;
num1=son.getTim e();
What's son?
last=ctime(&num 1);
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<iostre am>
#include<strstr eam>
using namespace std;

class TimeStamp{

time_t zaman;
public:

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

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

cout<<ctime(&za man)<<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,m in,sec,yil;
char arr[3];
char yilim[5];
public:
void zamanyazdir()
{
ilk.setZaman();
for(int i=0;i<100000000 0;i++){}//for the times to be different
son.setZaman();
}

void zamanFarki()
{

cout<<difftime( son.getZaman(), ilk.getZaman()) <<endl;

}

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

sayi1=son.getZa man();
cout<<"birinci: "<<birinci<<end l;//1
cout.flush();
fflush(stdin);
sonuncu=ctime(& sayi1);
cout<<"birinci: "<<birinci<<end l;//2
cout<<"sonuncu: "<<sonuncu<<end l;//3
//1 and 2 prints different values

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

void main(){

Task myTask;
myTask.zamanyaz dir();
myTask.kopyala( );
myTask.zamanFar ki();
myTask.ctimeOla rak();


}

Oct 29 '05 #3
berkay wrote:
here is the whole program
#include <ctime>
#include<iostre am>
#include<strstr eam>
using namespace std;

class TimeStamp{

time_t zaman;
public:

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

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

cout<<ctime(&za man)<<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,m in,sec,yil;
char arr[3];
char yilim[5];
public:
void zamanyazdir()
{
ilk.setZaman();
for(int i=0;i<100000000 0;i++){}//for the times to be different
son.setZaman();
}

void zamanFarki()
{

cout<<difftime( son.getZaman(), ilk.getZaman()) <<endl;

}

void kopyala()
{
sayi=ilk.getZam an();
A time_t is not a long!
birinci=ctime(& sayi);
cout.flush();
fflush(stdin);

sayi1=son.getZa man();
cout<<"birinci: "<<birinci<<end l;//1
cout.flush();
fflush(stdin);
sonuncu=ctime(& sayi1);
cout<<"birinci: "<<birinci<<end l;//2
cout<<"sonuncu: "<<sonuncu<<end l;//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<100000000 0;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
2429
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 to take two of those objects and perform calculations on them like: the time difference, summing them, getting the mean time between events, etc. Is ctime what I should be using? Is there a better way?
1
574
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 #--------------------------- #include <io.h> #include <ctime> #include <string>
2
2958
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 interpreted as an absolute time value, it represents the number of seconds elapsed since 00:00:00 on January 1, 1970, Coordinated Universal Time (UTC). Now I need to use the function of ctime() in linux kernel, but it
2
18875
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
9366
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
3551
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 incorrect and would like a second opion. #include <time.h> #include <stdio.h>
4
3384
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
4128
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
5482
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 that I would implement myself if needed. I have started programming c++ long before I did Visual Basic, and even before Visual Basic was around. However, one has to give credit, where credit is due: What's wrong with the (imo really good) VB...
0
9562
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
10285
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10063
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9114
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7598
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6838
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5494
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5622
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4270
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.