hello, I have a question about ctime library

I want to get the time difference between certain moments of objects life but it seems that variables in them stay unchanged!
// variables
time_t start;
time_t no_infection;
bool time_already_set = 0;
bool infected_or_not = 0;
int not_infected_t = 0;

// related functions

void bacteria::set_no_infection() {
if (infected_or_not == 0)
no_infection = clock();
}

//For the start I have

void bacteria::set_time() {
if (infected_or_not == 1 && time_already_set!=1) {
start = clock();
time_already_set = 1;
}
}
//It seems that time variables do not change during the program I test it using get functions

double bacteria::get_time() {
if (infected_or_not == 1)
return ((clock() - start) / CLOCKS_PER_SEC);
else
return -1;
}
int bacteria::get_no_infection() {
if (infected_or_not = 0)
return ((clock() - no_infection) / CLOCKS_PER_SEC);
else
return -1;
}

//In main program I test it like this:

while (1) {
for (int i = 0; i < b.size() - 1; i++) {
bactpop[i].set_no_infection();
bactpop[i].inf(phagepop[i], bactpop[i], p);
bactpop[i].kill_the_bacteria(b, i);
cout << " " << b[i].start << " " << b[i].no_infection << endl;
}
cout << p.size() << " " << b.size() << endl;
}
Last edited on
The divisions by CLOCKS_PER_SEC are integer divisions. You could define for example a const double double_clocks_per_sec = CLOCKS_PER_SEC; and divide by that, instead. That would force floating point division.
Last edited on
I get the same issue. maybe rounding is ok here i need not double precision.
Also, this:
if (infected_or_not = 0)
should be:
if (infected_or_not == 0)
thank you very much!!!
Topic archived. No new replies allowed.