I'm in need of help writing my clock function. While my car is on, gasoline will be subtracted by 0.05 gallons every 1000 milliseconds. My issue is setting up the order of the start and end time. Any help would be much appreciated! I've bolded all functions related to the issue.
#include <chrono> //this should make the below ones pointless?
#include <ctime> //these 2 are the same, except the second one is for C.
#include<time.h>
looks like you bolded the right thing. start car and stop car (use consistent names?) aka shut off should start and stop your timer/clock.
Its your program and design but it seems like your forever while loop should have a start car option so you can turn it off and on (?).
as it stands, you appear to start it and then they can shut it off but shut off does not get you out of that while loop, and they can't restart it, ??? not sure what you are going for there.
regardless, if you start and stop the clock when you start and stop the car, it will drain the gas. But you need that code too -- the while loop or somewhere else you need to *check* the time and every second reduce the gas amount. It has to be tied to the loop iteration (or in its own timer or thread type construct).
clock_t begin_time = clock(); //note: not a constant
while (1) {
c = getch();
clock_t current_time = clock();
double elapsed = (begin_time - current_time) / CLOCKS_PER_SEC;
userCar.update(elapsed); //updates the km travelled and the gas consumption
switch (c){
//...
}
begin_time = current_time; //for the next iteration
}
`getch()' blocks your program, ¿is that what you want?
`clock()' should only account for processor time, it is not a wall clock.
however, windows implementation seems to simply ignore the specifications and you do get a wall clock