Code not printing the things I want it to print

Hello! I am having issues printing the correct things. My teacher asked me to create a class that describes a passenger plane's properties and allows the plane to fly, land, and board and disembark passengers. To write the correct instructions, "In this project, you will write a class named Plane that has the following properties, each represented by an instance variable. All instance variables should be private.

inflight – a boolean variable indicating whether the plane is currently in flight
passengers – an unsigned int for the number of passengers currently onboard plane
capacity – an unsigned int for the maximum number of passengers the plane can hold
description - a string with the name of the current owner / operator of the plane and its model (for example, Air Canada 747).
In addition, the class should have the following member functions. All functions should be public.

Plane(int, string). The constructor should accept the plane's passenger capacity and the description of the plane.
The default value for the capacity should be 10
The default value for the description should be "(none)"
It should also initialize the inflight variable to false and the number of passengers to 0. Make the constructor an in-line function.

Notice that because this function has two parameters which both have default values, this also serves as the default constructor.

Accessors. Appropriate "getter" functions should be supplied for each of the instance variables. For example, you should have a function named getPassengers which takes no parameters and returns the number of passengers currently onboard. Each accessor should be an in-line function.
Modifiers (or mutators). Supplier "setter" functions only for the description and capacity instance variables. For example, you should have a function named setCapacity which takes an unsigned int parameter used to set the maximum number of passengers allowed on the plane, and have a void return type. Each modifier should be an in-line function.
void fly(). This function takes no parameters and sets the value of the inflight instance variable to true. (It should print an error message if the plane is already flying.) Make this function definition external to the class definition.
void land(). This function takes no parameters and sets the value of the inflight instance variable to false. (It should print an error message if the plane is not flying.) Its definition should be external to the Plane class definition (not in-line).
void board(unsigned int). This function adds the number passed as a parameter to the number of passengers already onboard. It should not allow the number of passengers to exceed the capacity of the plane (if it does, print an error message and set the number of passengers to capacity). If the plane is inflight, then print an error message and do not update the number of passengers. Make this function external to the Plane class definition.
void disembark(unsigned int). This function subtracts the number passed as a parameter from the number of passengers already on board. If the resulting number of passengers would be a negative number, then set passengers to 0 (this is not an error). If the plane is inflight, then print an error message do not update the number of passengers. Make this function definition external to the class definition.
void show(). This function displays all properties of the plane, for example, if a Airbus 320 out of France plane has a capacity of 162 passengers, and is currently in-flight with 139 passengers"

Sorry for posting the instructions, but on my previous post someone recommended me to post them with the post. After the instructions, the teacher asked me to "Demonstrate your class by writing a main function which does the following:

Create a United 737 plane with a capacity of 285 passengers (set the values using the constructor)
Board the first 20 passengers .
Board another 100 passengers.
Board another 121 passengers.
Take off for New York.
Use the show function to print the current state of the plane.
Land the plane.
Disembark all passengers (use disembark(9999), which should empty the plane).
Show the status of the plane again."
That is the stuff I tried to do in main. I know there is some issue with the variables, but I can't seem to find the issue. Thank you, anything would help.

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
#include <iostream>

using namespace std;
class Plane;
void Plane(int capacity, string description);
void land();
void fly();
void board(unsigned int);
void disembark(unsigned int);
void show();
int getPassengers();
int setCapacity();
int main()
{
	getPassengers();
	setCapacity();
	string description = "United 737";
	int passengers = 20;
	passengers += 100;
	passengers += 121;
	fly();
	show();
	land();
	disembark(9999);
	show();
}

class Plane { 
private:
	bool inflight;
	unsigned int passengers;
	unsigned int capacity;
	string description;
public:
	Plane(int capacity, string description)
	{
	bool inflight = false;
	int passengers;
	}
	int getPassengers();
	int setCapacity();
	string setDescription();
	void fly();
	void land();
	void board(unsigned int);
	void disembark(unsigned int);
	void show();
};



int getPassengers()
{
	int passengers;
}

int setCapacity()
{
	int capacity = 10;
}

string setDescription()
{
	string description = "";
}

void fly()
{
	bool inflight = true;
}
void land()
{
	bool inflight = false;
}

void board(unsigned int)
{
	bool inflight;
	int passengers;
	int capacity;
	if (inflight = true)
	{
		cout << "ERROR" << endl;
	}
	else if (inflight =  false)
	{
		if (passengers > capacity)
		{
			cout << "ERROR" << endl;
			passengers = capacity;
		}
	}
}

void disembark(unsigned int)
{
	int passengers;
	if (passengers < 0)
	{
		cout << "ERROR" << endl;	
	}
	else if (passengers == 9999)
	{
		passengers = 0;
	}
	else if (passengers >=0)
	{
		passengers -= passengers;
	}
}

void show()
{
	land();
	fly();
	bool inflight;
	string description;
	int capacity;
	int passengers;
	if (inflight = true)
	{
		cout << "The status of the first plane is: " << endl;
		cout << "The " << description << " (cap. " << capacity << ") is in flight with " << passengers << " passengers." << endl;
	}
	else if (inflight = false)
	{
		cout << "After landing and disembarking, the status of the plane is: " << endl;
		cout << "The " << description << " (cap. " << capacity << ") is in landed with no passengers." << endl;
	}
}
1
2
3
4
5
6
7
8
9
10
11
12

if (inflight = true)
	{
		cout << "The status of the first plane is: " << endl;
		cout << "The " << description << " (cap. " << capacity << ") is in flight with " << passengers << " passengers." << endl;
	}
	else if (inflight = false)
	{
		cout << "After landing and disembarking, the status of the plane is: " << endl;
		cout << "The " << description << " (cap. " << capacity << ") is in landed with no passengers." << endl;
	}


comparison is "==" not "=" so change inflight to == instead of =, also you could just simply write if(inflight) and if(!inflight) since they are boolean values.

also

in your function definitions you have to name your variable in parameters,

void board(unsigned int);

this is only valid in a function declaration, but you have a definition

so it should look something like this void board(unsigned int a){}
Last edited on
show() should just output the status of the plane. i.e take the member variables, process them and print them out

Operations of the plane like fly() and land() are not part of it. They should appear in main. eg if you create a plane p,

p.fly()
p.show()

p.land()
p.show()
I'll expand on adam2016's post a little bit.

The reason you use == instead of = is because = is called the assignment operator. This means that what ever is on the left hand side of the = is assigned what is on the right hand side. == is the comparison operator. This checks the left hand side first, and then compares the result of the left hand side to the right hand side. An easy way to remember is that when you are comparing you are comparing 2 things; that is why you have 2 equals.



Be aware, member variables are another name for instance variables.

In your constructor for the plane...
1
2
3
4
5
Plane(int capacity, string description)
	{
	bool inflight = false;
	int passengers;
	}

...Anytime you have a variable type (int, double, bool, string, Plane, etc) as the first part of a line, it creates a new variable that is separate from other variable with the same name, but in a different scope. The constructor is assigning false to a newly created variable that is separate from your member variable. This variable is in a local scope and gets destroyed when the constructor ends. Your member variables are in a scope called the instance scope.

It is a good idea to initialize passengers to 0. Not all compilers will do this for you, and if you try to access it, you may get a plane that has a negative number of passengers when you check, or passengers may be above the capacity; passengers may be random. It is a good idea to always initialize variables, and at the very least make sure that variables are initialized before accessing them.

You also need to initialize capacity and description by assigning to the members of the class. You can use an initializer list or use assignment. Just because you have the variables with the same name in the constructor definition doesn't mean they are being assigned to the members.

On another note, it is a good idea to think about how variables can make your class illogical and prevent the variable from becoming illogical.

Your accessors do not return a value...
1
2
3
4
int getPassengers()
{
	int passengers;
}

...This code creates a int named passengers and when the method is done, the variable you just created is destroyed. You can return the member variable using the "return" keyword.

Your setters are doing something similar...
1
2
3
4
int setCapacity()
{
	int capacity = 10;
}

... This is creating a local variable (not the member variable with the same name), and setting it's value to 10. Then the local variable with 10 assigned to it gets destroyed.

If you remove int, every time you call setCapacity() you always assign capacity to 10. What if you want to assign it to 20? I'm not sure if that is needed. If so, you need to pass in an int through the method declaration like you did for your constructor. Also, is setCapacity supposed to return an int? If you don't return a value from a method, you should change the return type to "void". This means nothing is returned in this context.

You can access the member variables declared in the private section of your class. In fact, you can access any member variable. For example, to assign inflight from a method, you could use the line...
 
inflight = true;

...This assigns true to the member variable named inflight.

I don't mean to be rude, but are you paying attention and taking notes in class, or is the professor just throwing you homework without explaining anything?
Last edited on
On closer inspection and without doing the whole thing this shows you how to properly structure the Plane class along with the Plane:: methods and how to use them.

Hint: Boarding and landing etc also involve adjusting passenger numbers, the capacity can be used to prevent over-booking(?) but doesn't change of course.

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
#include <iostream>

using namespace std;

class Plane
{
private:
    bool inflight;
    unsigned int passengers;
    unsigned int capacity;
    string description;
    
public:
    Plane(int aCapacity, string aDescription, int no_passengers)
    {
        capacity = aCapacity;
        description = aDescription;
        passengers = no_passengers;
        
        inflight = false;
    }
    
    ~Plane(){}
    
    int getPassengers();
    void setCapacity(int);
    void setDescription(string);
    void fly();
    void land();
    void board(unsigned int);
    void disembark(unsigned int);
    void show();
};

int Plane::getPassengers()
{
    return passengers;
}

void Plane::setCapacity(int cap)
{
    capacity = cap;
}

void Plane::setDescription(string some_description)
{
    description = some_description;
}

void Plane::fly()
{
    inflight = true;
}

void Plane::land()
{
    inflight = false;
}



void Plane::show()
{
    cout << "The status of "<< description << " is: " << endl;
    if (inflight == true)
    {
        cout << "The " << description << " (cap. " << capacity << ") is in flight with " << passengers << " passengers." << endl;
    }
    else if (inflight == false)
    {
        cout << "After landing and disembarking, the status of the plane is: " << endl;
        cout << "The " << description << " (cap. " << capacity << ") has landed with no passengers." << endl;
    }
}

int main()
{
    Plane aero_1(500, "United 737", 200);
    aero_1.fly();
    aero_1.show();
    
    
    Plane flight_27(300, "Lufthansa A380", 314);
    flight_27.fly();
    flight_27.show();
    
    flight_27.land();
    flight_27.show();
}

Forgot, this is what it does:
The status of United 737 is: 
The United 737 (cap. 500) is in flight with 200 passengers.
The status of Lufthansa A380 is: 
The Lufthansa A380 (cap. 300) is in flight with 314 passengers.
The status of Lufthansa A380 is: 
After landing and disembarking, the status of the plane is: 
The Lufthansa A380 (cap. 300) has landed with no passengers.
Program ended with exit code: 0
An except of where this (modified & extended) can lead:
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
    // Create a United 737 plane with a capacity of 285 passengers
    // (set the values using the constructor)
    
    Plane U737("United 737", 285);
    
    // Board the passengers.
    U737.board(20);
    U737.board(100);
    U737.board(121);
    
    // Take off (for New York).
    U737.fly();
    // Use the show function to print the current state of the plane.
    U737.show();
    
    U737.disembark(8);
    U737.show();
    
    // Land the plane.
    U737.land();
    U737.disembark(12);
    U737.show();
    
    // Disembark all (remaining) passengers (use disembark(9999)
    U737.disembark(9999);
    
    //Show the status of the plane again.
    U737.show();



The status of United 737 is:
       Capacity: 285
 No. Passengers: 241
  Flight status:  *** INFLIGHT
===========================

IMPOSSIBLE there are no parachutes
The status of United 737 is:
       Capacity: 285
 No. Passengers: 241
  Flight status:  *** INFLIGHT
===========================

The status of United 737 is:
       Capacity: 285
 No. Passengers: 229
  Flight status:  *** LANDED
===========================

The status of United 737 is:
       Capacity: 285
 No. Passengers: 0
  Flight status:  *** LANDED
===========================

Program ended with exit code: 0
Topic archived. No new replies allowed.