Program freezing up, not accepting inputs.

Mar 3, 2020 at 9:48pm
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
#include<iostream>
#include<conio.h>
#include <chrono>
#include <ctime>

using namespace std;
typedef std::chrono::high_resolution_clock Clock;
/*struct*/ class Car
{
private:

	int carYear;
	string carMake;
	string carModel;
	int carSpeed;
	float carGallons;
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;
	}
	Car()
	{
		carMake = "Bugatti";
		carYear = 2020;
		carModel = "Chiron";
	}
	int getCarYear() { return carYear; }
	string getCarMake() { return carMake; }
	string getCarModel() { return carModel; }
	int getCarSpeed() { return carSpeed; }
	float getCarGallons() { return carGallons; }
	void setCarSpeed(int cS) { carSpeed = cS; }
	void setCarGallons(float cG) { carGallons = cG; }
	void accelerate()
	{
		startCar();
		if(carGallons>0)
		{ 
		carSpeed + 5;
		carGallons - .5;
		}
		else if (carGallons < 0)
		{
			carGallons = 0;
			cout << "Please refill tank!";
		}
	}
	void brake()
	{
		startCar();
		if(carSpeed>0&&carGallons>0)
		{ 
		carSpeed - 5;
		carGallons - .2;
		}
		else if(carSpeed<0&&carGallons<0)
		{
			carSpeed = 0;

			cout << "Your car isn't moving!";
		}
	}
	void fillUP()
	{
		
		if (carSpeed > 0)
		{
			cout << "The car is still moving!";
		}
		else if(carSpeed==0)
		{
		if (carGallons > 22)
		{
			cout << "Car is full!";
			carGallons = 22;
		}
		else
		{

		carGallons + .2;
		}
		}
	}
	void startCar(bool carOn=true)
	{
		
		while (true)
		{
			std::chrono::system_clock::time_point then;
			auto now = std::chrono::system_clock::now();
			auto duration = now - then;
			then = now;
			if (false) return;
			{
			auto s = std::chrono::duration_cast<std::chrono::duration<double>>(duration);
			carGallons -= s.count() * .05f;
		}
		}
	}
};

int main()
{
	
	 bool carOn = false;
	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;
	if (ch== 'o'|| ch=='O') {
		bool carOn = true;
		cout << "\nCar is now on! Safe driving!\n";
	int c = 0;
	while (1)
	{
		c = 0;
		switch (c = _getch())
		{
			
		case 32:
			userCar.fillUP();
			break;
		case 72:
			cout << userCar.getCarSpeed() << " " << userCar.getCarGallons();
			userCar.accelerate();
			break;

		case 80:
			userCar.brake();
			break;

		case 75:cout << "\nleft"; break;
		case 77:cout << "\nright"; break;
		default:
			cout << " ";
		}
	}

}
	else
	{
		cout << "\nCar is not on!";
	}
		return 0;
}


Start your new 2020 Bugatti Chiron!
Hit O to start your Engine!o

Car is now on! Safe driving!



So it isn't accepting any input after turning on the car.
Last edited on Mar 3, 2020 at 9:54pm
Mar 4, 2020 at 6:12am
_getch()?

Why are you using that?
Mar 4, 2020 at 10:09pm
It's grabbing the character value, then whatever that value is correspondences to a key. In this case, each of the cases denotes an arrow key and space bar.
Mar 5, 2020 at 12:44pm
When will startCar() return?
Mar 5, 2020 at 11:21pm
probably never, I need help writing a function that subtracts gasoline while the car is on
Mar 6, 2020 at 2:53pm
1
2
3
4
5
6
7
8
9
10
11
12
13
int main(){
   while(true){
      userCar.update();
      //manage input
   }
}

void userCar::update(){
   if(this->is_on())
      gas -= 42;
   if(gas <= 0)
      this->turn_off();
}


also, avoid those magic numbers in your switch
Last edited on Mar 6, 2020 at 2:55pm
Topic archived. No new replies allowed.