Running clock? Subtracting gas?

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.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
#include<iostream>
#include<conio.h>
#include <chrono>
#include <ctime>
#include<time.h>
#include <thread>
#include <Windows.h>
#include <future>
using namespace std;
/*struct*/ class Car
{
private:
 
	int carYear;
	string carMake;
	string carModel;
	int carSpeed;
	float carGallons;
	bool engine;
public:
 
	Car(int cY = 0, string cMa = "", string cMo = "", int cS = 0, float cG = 10)
	{
		carYear = cY;
		carMake = cMa;
		carModel = cMo;
		carSpeed = cS;
		carGallons = cG;
		engine = false;
	}
 
	
	
	int getCarYear() { return carYear; }
	string getCarMake() { return carMake; }
	string getCarModel() { return carModel; }
	int getCarSpeed() { return carSpeed; }
	bool getBool(){return engine;
	}
	float getCarGallons() { return carGallons; }
	void setCarSpeed(int cS) { carSpeed = cS; }
	void setCarGallons(float cG) { carGallons = cG; }
	
	void accelerate()
	{
		if (carGallons > 0)
		{
			carSpeed += 5;
			carGallons -= .5;
		}
		else
		{
			cout << "Please refill tank!";
		}
	}
	void brake()
	{
		if (carSpeed > 0 && carGallons > 0)
		{
			carSpeed -= 5;
			carGallons -= .2;
		}
		else if (carSpeed <= 0 || carGallons < 0)
		{
			carSpeed = 0;
 
			cout << "Your car isn't moving!";
		}
	}
	void startCar()
	{
		engine = true;
 
	}
	
	void shutOff(){
		if(carSpeed > 0){
			cout << "Please stop first";
		}else{
			engine = false;
			cout << "Engine is off";
		}
	}
	
	void fillUp(){
		if(carSpeed == 0 && carGallons <= 21.5){
			carGallons += 0.5;
		}
		else if(carSpeed > 0){
			cout << "Need to stop first";
		}
		else if(carGallons >= 22){
			carGallons=22;
			cout << "Cannot fill anymore";
		}
	}
};
 
#define KEY_UP 72 //Accelerate
#define KEY_DOWN 80 // Brake
#define KEY_LEFT 75 // Fill up
#define KEY_RIGHT 77 // Shut Off
 
int main()
{
float currentGallons, newGallons;
	Car userCar(2020, "Bugatti", "Chiron");
	cout << "\nStart your new " << userCar.getCarYear() << " " << userCar.getCarMake() << " " << userCar.getCarModel() << "!";
	cout << "\nHit O to start your Engine!";
	char ch;
	cin >> ch;
		//userCar.startCar();
int c;
 
	
    
    	if (ch == 'o' || ch == 'O') 
{
		
		userCar.startCar();
  const clock_t begin_time = clock();
    while(1)
    {
    
    	// time1 = current time
 //newGallons = currentGallons - ((clock () - begin_time) * 0.05);
 //currentGallons=userCar.getCarGallons();
        switch((c=getch())) 
		{
			if(userCar.getBool()==true)
			{
			
        case KEY_UP:
            cout << "Accelerate" << endl;
            userCar.accelerate();
            cout << userCar.getCarSpeed() << endl;
            cout << userCar.getCarGallons();
            cout << endl;
            break;
        case KEY_DOWN:
            cout << "Braking" << endl;
            userCar.brake();
            cout << userCar.getCarSpeed() << endl;
            cout << userCar.getCarGallons() << endl;
            cout << endl;
            break;
			}
        case KEY_LEFT:
            cout << "Filling up gas";
            userCar.fillUp();
            cout << userCar.getCarGallons() << endl;
            cout << endl;
            break;
        case KEY_RIGHT:
        	cout << "Shutting Off";
            userCar.shutOff();
            break;
        
		}
        
    }
        
		
		
		cout << float( clock () - begin_time ) /  CLOCKS_PER_SEC;
		}
		else
	{
		cout << "\nCar is not on!";
	}
        //time2 = current time
        
        //totalTime = time2 - time1
        
    
        //currentGallons = userCar.getCarGallons();
        
        // newGallons = currentGallons - (totalTime * 0.05);
        
        if (userCar.getBool()==true)
        {
       
        userCar.setCarGallons(newGallons) ;
		}
	return 0;
 
    }





Last edited on
#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).
One problem is that you can fill up the gas inside the loop, but you don't consume gas there!

Each time through the loop you should update the fuel consumption based on the elapsed time since the last time through the loop.
1
2
3
4
5
6
7
8
9
10
11
12
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
Last edited on
Topic archived. No new replies allowed.