boolean return problem

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
#include <iostream>
#include <string>
using namespace std;

class Vehicle
{
  public:
  int weight;
  int top_speed;
  long driven_km;

  Vehicle(int weight, int top_speed, long driven_km);
  void Drive(int journey);
  int InputWeight();
  int InputTopSpeed();
  long InputDrivenKM();

};
  
Vehicle::Vehicle(int Aw, int Ats, long Akm)

{

  weight = Aw;

  top_speed = Ats;

  driven_km = Akm;

}
  

void Vehicle::Drive(int journey)

{

  driven_km += journey;

}
  

int Vehicle::InputWeight()

{

  return weight;

}
  

int Vehicle::InputTopSpeed()

{

  return top_speed;

}
  

long Vehicle::InputDrivenKM()

{

  return driven_km;

}
  

class Car : public Vehicle 
{
public:
	string brand;
	string model;
	string register_no;
	bool running;
		
	Car(int weight, int top_speed, long driven_km, 
		 string car_brand, string car_model, string car_register_no, bool car_running);
	
	bool turn_off() {return (running);}
	bool turn_on() {return (running);}
	void check_up();
	
};

Car::Car(int weight, int top_speed, long driven_km, 
		 string car_brand, string car_model, string car_register_no, bool car_running):
Vehicle(weight,top_speed, driven_km)
{
	brand = car_brand;
	model = car_model;
	register_no = car_register_no;
	running = car_running;
}

void Car::check_up()
{
	cout << "car info:" << endl;
	cout << "brand:" << brand << endl;
	cout << "model:" << model << endl;
	cout << "Kilometres:" << driven_km << endl;
	cout << "weight:" << weight << endl;
	cout << "Top speed:" << top_speed << endl;
	cout << "License plate:" << register_no << endl;
	if (running = false) {
	cout << "car is not running" << endl;
	}
	else if (running = true) {
	cout << "car is running" << endl;
	}
}

int main()
{

  int weight, speed;
  long km;
  string brand, model, license;
  
  cout << "Input car brand: ";
  cin >> brand;
 
  cout << "Input car model: ";
  cin >> model; 

  cout << "Input car license plate number: ";
  cin >> license;

  cout << "Input car weight: ";
  cin >> weight; 
 
  cout << "Input car top speed: ";
  cin >> speed; 

  cout << "Input distance traveled by car: ";
  cin >> km; 

  cout << endl;

  Car carX(weight, speed, km, brand, model, license, 0);
  carX.check_up();
  carX.turn_on();
  carX.Drive(95);
  cout << endl;
  carX.check_up();
}


output should be like

car info:
brand:Nissan
model:Primera
Kilometres:2540000
weight:1340
Top speed:187
License plate:EFS-144
car is not running.
 
car info:
brand:Nissan
model:Primera
Kilometres:2540095
weight:1340
Top speed:187
License plate:EFS-144
car is running.


But it always says that car is running instead of car is not running and only then - running.

Where is the problem? Why running is always set 1?
1
2
3
4
5
6
if (running = false) {
	cout << "car is not running" << endl;
	}
	else if (running = true) {
	cout << "car is running" << endl;
	}


These should be == (comparing 2 values) not = (assignment)
Topic archived. No new replies allowed.