object oriented programming

hello everyone, I am facing a problem in displaying the output
here is the code

#include <iostream>
#include <iomanip>
#include <string>
using namespace std;

class Audience{
protected:
string name;
int age, seatNo;
public:
Audience(string name, int age, int seatNo)
{
this->name = name;
this->age = age;
this->seatNo=seatNo;
}
int getAge() const
{
return age;
}
virtual void displayDetails()
{

}
virtual double calcDisc() = 0;
};

class Adults: public Audience{ //inheritance
public:
Adults(string name="", int age=0, int seatNo=0): Audience(name, age, seatNo)
{}
void displayDetails()
{
cout << setw(30) << name << setw(5) << age
<< setw(40) << "-" << setw(9) << seatNo << endl;
}
double calcDisc()
{
if (age >= 60)
return 20;
}
};

class Kids: public Audience{ //inheritance
private:
string parentName;
public:
Kids(string name="", int age=0, int seatNo=0, string parentName=""):Audience(name, age, seatNo)
{
this->parentName = parentName;
}
void displayDetails()
{
cout << setw(30) << name << setw(5) << age
<< setw(40) << parentName << setw(9) << seatNo << endl;
}
double calcDisc()
{
double discount;

if(age <= 2)
{
discount = 100;
}
else if(age <= 12)
{
discount = 20;
}

return discount;
}
};

class Date{
private:
int day, month, year;
public:
void dispDate()
{
cout << setprecision(2) << day << "-" << setprecision(2) << month << "-" << setprecision(4) << year;
}
void readDate()
{
char dummy;
cin >> day >> dummy >> month >> dummy >> year;
}
};

class Time{
private:
int hour, minute;
public:
void dispTime()
{
string time;
if (hour <= 11)
{
time = "am";
}
else
{
time = "pm";
}

cout << hour << ":" << minute << " " << time;
}
void readTime()
{
char dummy;
cin >> hour >> dummy >> minute;
}
};

class Movie{
private:
string title;
bool seat[50];
double price;
int numAdults, numKids;
int numAudience, hallNo;
Audience* audienceList[20]; //aggregation with class audience
Date d;
Time t;
public:
Movie()
{
numAdults = 0;
numKids = 0;
numAudience = 0;
}

string getTitle()
{
return title;
}

bool getSeat(int s)
{
bool value;

if(seat[s+1] == true) //check whether array have value or not
{
value = false;
}
else
{
value = true;
}
return value;
}

void setSeat(int s)
{
s = seat[s+1];
}

void addAudience(Audience* audience)
{
audienceList[numAudience] = audience;

if (audience->getAge()<=12)
{
numKids++;
}
else
{
numAdults++;
}

numAudience++;
}

void getDateTime()
{
cout << "(" ;
d.dispDate();
cout << ", ";
t.dispTime();
cout << ")" << endl;
}

void readInput()
{
cout << "Title: ";
cin.ignore();
getline(cin, title);
cout << "Hall: ";
cin >> hallNo;
cout << "Ticket price: RM";
cin >> price;
cout << "Date [dd-mm-yyyy]: ";
d.readDate();
cout << "Time [hour:min] using 24-hour format: ";
t.readTime();
}

void displayInfoMovie()
{
cout << setw(7) << "Title" << ": " << title << endl;
cout << setw(7) << "Hall" << ": " << hallNo << endl;
cout << setw(7) << "Date" << ": ";
d.dispDate();
cout << endl;
cout << setw(7) << "Time" << ": ";
t.dispTime();
cout << endl;
cout << setw(7) << "Price" << ": RM" << fixed << setprecision(2) << price << endl;
}

void displayInfoAudience()
{
if (numAudience == 0)
{
cout << "No audience!" << endl;
}
else
{
cout << setw(18) << "Number of Audience" << ": " << numAudience << endl
<< setw(18) << "Number of Adults" << ": " << numAdults << endl
<< setw(18) << "Number of Kids" << ": " << numKids << endl;

cout << endl
<< setw(4) << "No" << setw(30) << "Name" << setw(5) << "Age"
<< setw(40) << "Parent Name" << setw(9) << "Seat No." << "Ticket (RM)" << endl;

double total = 0;

for (int i=0; i<numAudience; i++)
{
cout << setw(4) << i+1 ;
audienceList[i]->displayDetails();
price *= audienceList[i]->calcDisc();
cout << fixed << setprecision(2) << price << endl;
cout << endl;
total += price;
}

cout << "Total ticket price = RM" << fixed << setprecision(2) << total << endl;
}
}
};

int displayMenu()
{
int choice;
cout << "========== Menu ==========" << endl
<< endl
<< "[1] Add Movie" << endl
<< "[2] Add Audience" << endl
<< "[3] Display Movies" << endl
<< "[4] Display Audiences" << endl
<< "[5] Exit" << endl
<< "==========================" << endl
<< endl
<< "Select task: ";
cin >> choice;
cout << endl;

return choice;
}


int main()
{
int choice;
Movie movie[10];
int count = 0;
Audience *A;

choice = displayMenu();

while(choice >=1 && choice <= 5)
{
switch(choice)
{
case 1:
{
for(int i=0; i<=count; i++)
{
cout << "<<< Add Movie >>>" << endl;
movie[i].readInput();
}
count++;
break;
}

case 2:
{

char choice2 = 'y';
int choice3;
cout << "<<< Add Audience >>>" << endl;
cout << endl;
cout << "Movie List" << endl;

for(int i=0; i<count; i++)
{
cout << i+1 << ") " << movie[i].getTitle();
movie[i].getDateTime();
cout << endl;

cout << "Select movie: ";
cin >> choice3;

cout << endl
<< "--- Enter Audience Info ---" << endl;

string name, pname;
int age, seatNo;

while (choice2 == 'y')
{
cout << left;
cout << setw(7) << "Name" << ": ";
cin.ignore();
getline(cin, name);
cout << setw(7) << "Age" << ": ";
cin >> age;
if(age > 12)
{
cout << setw(7) << "Seat No" << ": ";
cin >> seatNo;
cout << endl;

if(movie[choice3+1].getSeat(seatNo) == false)
{
cout << "Seat unavailable, choose others!" << endl << endl;
cin >> seatNo;
}

Adults ad;
A = &ad;
movie[i].addAudience(A);
}
else
{
cout << "Parent Name: ";
cin >> pname;

Kids k;
A = &k;
movie[i].addAudience(A);

}

cout << endl
<< "Press 'Y' to continue >> ";
cin >> choice2;
cout << endl
<< "--- Enter Audience Info ---" << endl;
}

}



break;
}

case 3:
{
if (count == 0)
{
cout << "Sorry!! No movie data to display..." << endl;
}
else
{
cout << "<<< Movie Info >>>" << endl;
cout << endl
<< "Number of Movies: " << count << endl;

for(int i=0; i<count; i++)
{
cout << endl
<< "Movie #" << i+1;
movie[i].displayInfoAudience();
}
}
break;
}

case 4:
{
if (count == 0)
{
cout << "Sorry!! No movie, please add movie first..." << endl;
}
else
{
cout << "<<< Movie(s) and Audience(s) Info >>>" << endl;
cout << endl
<< "Number of Movies: " << count << endl;

for(int i=0; i<count; i++)
{
cout << endl
<< "Movie #" << i+1 << endl;
movie[i].displayInfoMovie();
cout << endl;
movie[i].displayInfoAudience();
}
}
break;
}

default:
{
cout << "Thank you! :)" << endl;
exit(0);
break;
}
}
cout << endl;
choice = displayMenu();
}

return 0;
}
There are number of problems with your program. The bottom line is it's not using OO properly. Unline Java, C++ isn't just an object oriented programming language, and from the C++ point of view, not everything is an object.

It would be helpful if you posted what you expect and what you see.

Also, welcome to the forum.

Please use the code format tags to format your code.
this should be the output after we enter the movie and value

========== Menu ==========
[1] Add Movie
[2] Add Audience
[3] Display Movies
[4] Display Audiences
[5] Exit
==========================
Select task: 4
<<< Movie(s) and Audience(s) Info >>>
Number of Movies: 1
Movie #1
Title: Jumanji 2
Hall : 3
Date : 30-05-2020
Time : 11:30 am
Price: RM28.00
Number of Audience: 2
Number of Adults : 1
Number of Kids : 1
No Name Age Parent Name Seat No Ticket (RM)
1. Afiqah Salim 28 - 4 28.00
2. Auni Nazri 2 Afiqah Salim 4 0.00
Total ticket price = RM28.00
but the output i get is without all the details like below:

========== Menu ==========
[1] Add Movie
[2] Add Audience
[3] Display Movies
[4] Display Audiences
[5] Exit
==========================
Select task: 4
<<< Movie(s) and Audience(s) Info >>>
Number of Movies: 1
Movie #1
Title: Jumanji 2
Hall : 3
Date : 30-05-2020
Time : 11:30 am
Price: RM28.00
Number of Audience: 2
Number of Adults : 1
Number of Kids : 1
No Name Age Parent Name Seat No Ticket (RM)
However, THERE IS A MAJOR PROBLEM with the lines .addAudience(). You're storing pointers in audienceList array. However these pointers are only valid for the scope of the associated objects ad and k. Once these go out of scope at the end of the block, then their addresses are no longer valid!! Also, the data for these objects are not set.

For a first revision, consider (not tested):

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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;

class Audience {
protected:
	string name;
	int age {}, seatNo {};

public:
	Audience() {}
	Audience(const string& name_, int age_, int seatNo_) : name(name_), age(age_), seatNo(seatNo_) {}
	virtual ~Audience() {}

	int getAge() const { return age; }
	virtual void displayDetails() const {}
	virtual double calcDisc() const = 0;
};

class Adults : public Audience {
public:
	Adults() {}
	Adults(const string& name_ , int age_, int seatNo_) : Audience(name_, age_, seatNo_) {}

	void displayDetails() const override
	{
		cout << setw(30) << name << setw(5) << age
			<< setw(40) << "-" << setw(9) << seatNo << '\n';
	}

	double calcDisc() const override
	{
		return age >= 60 ? 60 : age;
	}
};

class Kids : public Audience {
private:
	string parentName;

public:
	Kids() {}
	Kids(string name_, int age_, int seatNo_, const string& parentName_) :Audience(name_, age_, seatNo_), parentName(parentName_) {}
	void displayDetails() const override
	{
		cout << setw(30) << name << setw(5) << age
			<< setw(40) << parentName << setw(9) << seatNo << '\n';
	}

	double calcDisc() const override
	{
		double discount {};

		if (age <= 2)
			discount = 100;
		else if (age <= 12)
			discount = 20;

		return discount;
	}
};

class Date {
private:
	int day {}, month {}, year {};

public:
	void dispDate() const
	{
		cout << setprecision(2) << day << "-" << setprecision(2) << month << "-" << setprecision(4) << year;
	}

	void readDate()
	{
		char dummy {};

		cin >> day >> dummy >> month >> dummy >> year;
	}
};

class Time {
private:
	int hour {}, minute {};

public:
	void dispTime() const
	{
		const string time {hour <= 11 ? "am" : "pm"};

		cout << hour << ":" << minute << " " << time;
	}

	void readTime()
	{
		char dummy {};

		cin >> hour >> dummy >> minute;
	}
};

class Movie {
private:
	string title;
	bool seat[50] {};
	mutable double price {};
	int numAdults {}, numKids {};
	int numAudience {}, hallNo {};
	Audience* audienceList[20] {}; //aggregation with class audience
	Date d;
	Time t;

public:
	Movie() {}

	~Movie()
	{
		for (size_t a = 0; a < 20; ++a)
			delete audienceList[a];
	}

	string getTitle() const { return title; }

	bool getSeat(int s) const {	return !seat[s + 1]; }
	void setSeat(int s) { seat[s + 1] = true; }

	void addAudience(Audience* audience)
	{
		audienceList[numAudience] = audience;

		if (audience->getAge() <= 12)
			++numKids;
		else
			++numAdults;

		++numAudience;
	}

	void getDateTime() const
	{
		cout << "(";
		d.dispDate();

		cout << ", ";
		t.dispTime();

		cout << ")\n";
	}

	void readInput()
	{
		cout << "Title: ";
		cin.ignore();
		getline(cin, title);

		cout << "Hall: ";
		cin >> hallNo;

		cout << "Ticket price: RM";
		cin >> price;

		cout << "Date [dd-mm-yyyy]: ";
		d.readDate();

		cout << "Time [hour:min] using 24-hour format: ";
		t.readTime();
	}

	void displayInfoMovie() const
	{
		cout << setw(7) << "Title" << ": " << title << '\n';
		cout << setw(7) << "Hall" << ": " << hallNo << '\n';
		cout << setw(7) << "Date" << ": ";
		d.dispDate();
		cout << '\n';
		cout << setw(7) << "Time" << ": ";
		t.dispTime();
		cout << '\n';
		cout << setw(7) << "Price" << ": RM" << fixed << setprecision(2) << price << '\n';
	}

	void displayInfoAudience() const
	{
		if (numAudience == 0)
			cout << "No audience!\n";
		else {
			cout << setw(18) << "Number of Audience" << ": " << numAudience << '\n'
				<< setw(18) << "Number of Adults" << ": " << numAdults << '\n'
				<< setw(18) << "Number of Kids" << ": " << numKids << '\n';

			cout << '\n'
				<< setw(4) << "No" << setw(30) << "Name" << setw(5) << "Age"
				<< setw(40) << "Parent Name" << setw(9) << "Seat No." << "Ticket (RM)" << '\n';

			double total {};

			for (int i = 0; i < numAudience; ++i) {
				cout << setw(4) << i + 1;
				audienceList[i]->displayDetails();
				price *= audienceList[i]->calcDisc();
				cout << fixed << setprecision(2) << price << '\n';
				cout << '\n';
				total += price;
			}

			cout << "Total ticket price = RM" << fixed << setprecision(2) << total << '\n';
		}
	}
};

int displayMenu()
{
	int choice {};

	cout << "========== Menu ==========\n\n"
		<< "[1] Add Movie\n"
		<< "[2] Add Audience\n"
		<< "[3] Display Movies\n"
		<< "[4] Display Audiences\n"
		<< "[5] Exit\n"
		<< "==========================\n\n"
		<< "Select task: ";

	cin >> choice;
	cout << '\n';

	return choice;
}

int main()
{
	Movie movie[10];
	int count {};

	auto choice {displayMenu()};

	while (choice >= 1 && choice <= 5) {
		switch (choice) {
			case 1:
				for (int i = 0; i <= count; ++i) {
					cout << "<<< Add Movie >>>\n";
					movie[i].readInput();
				}

				++count;
				break;

			case 2:
			{
				char choice2 {'y'};
				int choice3 {};

				cout << "<<< Add Audience >>>\n";
				cout << '\n';
				cout << "Movie List\n";

				for (int i = 0; i < count; ++i) {
					cout << i + 1 << ") " << movie[i].getTitle();
					movie[i].getDateTime();
					cout << '\n';

					cout << "Select movie: ";
					cin >> choice3;

					cout << "\n--- Enter Audience Info ---\n";

					string name, pname;
					int age {}, seatNo {};

					while (choice2 == 'y') {
						cout << left;
						cout << setw(7) << "Name" << ": ";
						cin.ignore();
						getline(cin, name);

						cout << setw(7) << "Age" << ": ";
						cin >> age;

						if (age > 12) {
							cout << setw(7) << "Seat No" << ": ";
							cin >> seatNo;
							cout << '\n';

							if (movie[choice3 + 1].getSeat(seatNo) == false) {
								cout << "Seat unavailable, choose others!\n\n";
								cin >> seatNo;
							}

							movie[i].addAudience(new Adults(name, age, seatNo));
						} else {
							cout << "Parent Name: ";
							cin >> pname;

							movie[i].addAudience(new Kids (name, age, seatNo, pname));
						}

						cout << "\nPress 'Y' to continue >> ";
						cin >> choice2;
						cout << "\n--- Enter Audience Info ---\n";
					}
				}
				break;
			}

			case 3:
				if (count == 0)
					cout << "Sorry!! No movie data to display...\n";
				else {
					cout << "<<< Movie Info >>>\n";
					cout << "\nNumber of Movies: " << count << '\n';

					for (int i = 0; i < count; ++i) {
						cout << "\nMovie #" << i + 1;
						movie[i].displayInfoAudience();
					}
				}
				break;

			case 4:
				if (count == 0)
					cout << "Sorry!! No movie, please add movie first..." << endl;
				else {
					cout << "<<< Movie(s) and Audience(s) Info >>>\n";
					cout << "\nNumber of Movies: " << count << '\n';

					for (int i = 0; i < count; ++i) {
						cout << "\nMovie #" << i + 1 << '\n';
						movie[i].displayInfoMovie();
						cout << '\n';
						movie[i].displayInfoAudience();
					}
				}
				break;

			default:
				cout << "Thank you! :)\n";
				exit(0);
		}

		cout << '\n';
		choice = displayMenu();
	}
}


Last edited on
I had a test run the code and I get the price of the movie is dummy value?
and also I faced a problem in adding the movie and adding the audience, I have no idea how to deal with it
Well have a look at the code for Add Movie. The first time its called then count is 0 so the for loop executes once. The second time it's called then count is 1 and the for loop executes twice. Then the next time 3 times etc. This probably isn't what is wanted........
but I can take out the count since I need it to display all the output right?
is it we can use another loop instead of for loop?
here is the code i had modified

#include <iostream>
#include <iomanip>
#include <string>
using namespace std;

class Audience{
protected:
string name;
int age, seatNo;
public:
Audience(string name, int age, int seatNo)
{
this->name = name;
this->age = age;
this->seatNo=seatNo;
}
int getAge() const
{
return age;
}
virtual void displayDetails()
{

}
virtual double calcDisc() = 0;
};

class Adults: public Audience{ //inheritance
public:
Adults(string name="", int age=0, int seatNo=0): Audience(name, age, seatNo)
{}
void displayDetails()
{
cout << setw(20) << name << setw(8) << age
<< setw(20) << "-" << setw(8) << seatNo;
}
double calcDisc()
{
if (age >= 60)
return 0.8;
else
return 1;
}
};

class Kids: public Audience{ //inheritance
private:
string parentName;
public:
Kids(string name="", int age=0, int seatNo=0, string parentName=""):Audience(name, age, seatNo)
{
this->parentName = parentName;
}
void displayDetails()
{
cout << setw(20) << name << setw(8) << age
<< setw(20) << parentName << setw(8) << seatNo;
}
double calcDisc()
{
double discount;

if(age <= 2)
{
discount = 0;
}
else if(age <= 12)
{
discount = 0.8;
}

return discount;
}
};

class Date{
private:
int day, month, year;
public:
void dispDate()
{
cout << setprecision(2) << day << "-" << setprecision(2) << month << "-" << setprecision(4) << year;
}
void readDate()
{
char dummy;
cin >> day >> dummy >> month >> dummy >> year;
}
};

class Time{
private:
int hour, minute;
public:
void dispTime()
{
string time;
if (hour <= 11)
{
time = "am";
}
else
{
time = "pm";
}

cout << hour << ":" << minute << " " << time;
}
void readTime()
{
char dummy;
cin >> hour >> dummy >> minute;
}
};

class Movie{
private:
string title;
bool seat[50];
double price;
int numAdults, numKids;
int numAudience, hallNo;
Audience* audienceList[20]; //aggregation with class audience
Date d;
Time t;
public:
Movie()
{
price = 0.0;
numAdults = 0;
numKids = 0;
numAudience = 0;
}

string getTitle()
{
return title;
}

bool getSeat(int s)
{
bool value;

if(seat[s+1] == true) //check whether array have value or not
{
value = false;
}
else
{
value = true;
}
return value;
}

void setSeat(int s)
{
seat[s + 1] = true;
}

void addAudience(Audience* audience)
{
audienceList[numAudience] = audience;

if (audience->getAge() <= 12)
{
++numKids;
}
else
{
++numAdults;
}

++numAudience;
}

void getDateTime()
{
cout << "(" ;
d.dispDate();
cout << ", ";
t.dispTime();
cout << ")\n";
}

void readInput()
{
cout << "Title: ";
cin.ignore();
getline(cin, title);
cout << "Hall: ";
cin >> hallNo;
cout << "Ticket price: RM";
cin >> price;
cout << "Date [dd-mm-yyyy]: ";
d.readDate();
cout << "Time [hour:min] using 24-hour format: ";
t.readTime();
}

void displayInfoMovie()
{
cout << setw(7) << "Title" << ": " << title << endl;
cout << setw(7) << "Hall" << ": " << hallNo << endl;
cout << setw(7) << "Date" << ": ";
d.dispDate();
cout << endl;
cout << setw(7) << "Time" << ": ";
t.dispTime();
cout << endl;
cout << setw(7) << "Price" << ": RM" << fixed << setprecision(2) << price << endl;
}

void displayInfoAudience()
{
if (numAudience == 0)
{
cout << "No audience!" << endl;
}
else
{
cout << setw(18) << "Number of Audience" << ": " << numAudience << endl
<< setw(18) << "Number of Adults" << ": " << numAdults << endl
<< setw(18) << "Number of Kids" << ": " << numKids << endl;

cout << endl
<< "No" << setw(20) << "Name" << setw(8) << "Age"
<< setw(20) << "Parent Name" << setw(8) << "Seat No." << "Ticket (RM)" << endl;

double total = 0;

for (int i=0; i<numAudience; ++i)
{
cout << setw(4) << i+1;
audienceList[i]->displayDetails();
price = price * (audienceList[i]->calcDisc());
cout << fixed << setprecision(2) << price << endl;
total += price;
}

cout << "\nTotal ticket price = RM" << fixed << setprecision(2) << total << endl;
}
}
};

int displayMenu()
{
int choice;
cout << "========== Menu ==========\n"
<< "[1] Add Movie" << endl
<< "[2] Add Audience" << endl
<< "[3] Display Movies" << endl
<< "[4] Display Audiences" << endl
<< "[5] Exit" << endl
<< "==========================\n\n"
<< "Select task: ";
cin >> choice;

return choice;
}


int main()
{
int choice;
Movie movie[10];
int count = 0;

choice = displayMenu();

while(choice >=1 && choice <= 5)
{
switch(choice)
{
case 1:
{
for(int i=count; i<=count; ++i)
{
cout << "\n<<< Add Movie >>>" << endl;
movie[i].readInput();
}
count++;
break;
}

case 2:
{

char choice2 = 'y';
int choice3;
cout << "\n<<< Add Audience >>>" << endl;
cout << endl;
cout << "Movie List" << endl;

for(int i=0; i<count; ++i)
{
for(int j=0; j<count; ++j)
{
cout << j+1 << ") " << movie[j].getTitle();
movie[j].getDateTime();
}

cout << "Select movie: ";
cin >> choice3;

cout << endl
<< "--- Enter Audience Info ---" << endl;

string name, pname;
int age = 0, seatNo = 0;

while (choice2 == 'y')
{
cout << left;
cout << setw(7) << "Name" << ": ";
cin.ignore();
getline(cin, name);
cout << setw(7) << "Age" << ": ";
cin >> age;
if(age > 12)
{
cout << setw(7) << "Seat No" << ": ";
cin >> seatNo;

if(movie[choice3+1].getSeat(seatNo) == false)
{
cout << "Seat unavailable, choose others!" << endl << endl;
cin >> seatNo;
}

movie[i].addAudience(new Adults(name, age, seatNo));
}
else
{
cout << "Parent Name: ";
cin >> pname;

movie[i].addAudience(new Kids (name, age, seatNo, pname));
}

cout << endl
<< "Press 'Y' to continue >> ";
cin >> choice2;
// cout << endl
// << "--- Enter Audience Info ---" << endl;
}
}
break;
}

case 3:
{
if (count == 0)
{
cout << "Sorry!! No movie data to display..." << endl;
}
else
{
cout << "\n<<< Movie Info >>>" << endl;
cout << endl
<< "Number of Movies: " << count << endl;

for(int i=0; i<count; ++i)
{
cout << endl
<< "Movie #" << i+1 << endl;
movie[i].displayInfoMovie();
}
}
break;
}

case 4:
{
if (count == 0)
{
cout << "Sorry!! No movie, please add movie first..." << endl;
}
else
{
cout << "\n<<< Movie(s) and Audience(s) Info >>>" << endl;
cout << endl
<< "Number of Movies: " << count << endl;

for(int i=0; i<count; ++i)
{
cout << endl
<< "Movie #" << i+1 << endl;
movie[i].displayInfoMovie();
cout << endl;
movie[i].displayInfoAudience();
}
}
break;
}

default:
{
cout << "Thank you! :)" << endl;
exit(0);
}
}
cout << endl;
choice = displayMenu();
}

return 0;
}



now I had faced the problem with the case 2, I wonder why after I press n, it still show the movielist?
Use code tags when posting code so that its' readable!


[code]
... the code goes here
[/code]

thank you for your speedy reply! and I found another issue, the seat unavailable function does not show when the same value of seat is input by the user. and after I try all, the program end suddenly even I do not press option 5.

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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
#include  <iostream>
#include <iomanip>
#include <string>
using namespace std;

class Audience{
	protected:
		string name;
		int age, seatNo;
	public:
		Audience(string name, int age, int seatNo)
		{
			this->name = name;
			this->age = age;
			this->seatNo=seatNo;
		}
		int getAge() const
		{
			return age;
		}
		virtual void displayDetails()
		{
			
		}
		virtual double calcDisc() = 0;
};

class Adults: public Audience{	//inheritance
	public:
		Adults(string name="", int age=0, int seatNo=0): Audience(name, age, seatNo)
		{}
		void displayDetails()
		{
			cout << setw(20) << name << setw(8) << age
				 << setw(20) << "-" << setw(8) << seatNo;
		}
		double calcDisc()
		{
			if (age >= 60)
				return 0.8;
			else
				return 1;
		}
};

class Kids: public Audience{	//inheritance
	private:
		string parentName;
	public:
		Kids(string name="", int age=0, int seatNo=0, string parentName=""):Audience(name, age, seatNo)
		{
			this->parentName = parentName;
		}
		void displayDetails()
		{
			cout << setw(20) << name << setw(8) << age
				 << setw(20) << parentName << setw(8) << seatNo;
		}
		double calcDisc()
		{
			double discount;
			
			if(age <= 2)
			{
				discount = 0;
			}
			else if(age <= 12)
			{
				discount = 0.8;
			}
			
			return discount;
		}
};

class Date{
	private:
		int day, month, year;
	public:
		void dispDate()
		{
			cout << setprecision(2) << day << "-" << setprecision(2) << month << "-" << setprecision(4) << year;
		}
		void readDate()
		{
			char dummy;
			cin >> day >> dummy >> month >> dummy >> year;
		}
};

class Time{
	private:
		int hour, minute;
	public:
		void dispTime()
		{
			string time;
			if (hour <= 11)
			{
				time = "am";
			}
			else
			{
				time = "pm";
			}
			
			cout << hour << ":" << minute << " " << time;
		}
		void readTime()
		{
			char dummy;
			cin >> hour >> dummy >> minute;
		}
};

class Movie{
	private:
		string title;
		bool seat[50];
		double price;
		int numAdults, numKids;
		int numAudience, hallNo;
		Audience* audienceList[20];	//aggregation with class audience
		Date d;
		Time t;
	public:
		Movie()
		{
			price = 0.0;
			numAdults = 0;
			numKids = 0;
			numAudience = 0;
		}
		
		string getTitle()
		{
			return title;
		}
		
		bool getSeat(int s)	
		{
			bool value;
			
			if(seat[s+1] == true)	//check whether array have value or not
			{
				value = false;
			}
			else
			{
				value = true;
			}
			return value;
		}
		
		void setSeat(int s)	
		{
			s = seat[s+1];
		}
		
		void addAudience(Audience* audience)	
		{
			audienceList[numAudience] = audience;
			
			if (audience->getAge() <= 12)
			{
				++numKids;
			}
			else
			{
				++numAdults;
			}
			
			++numAudience;
		}
		
		void getDateTime()
		{
			cout << "(" ;
			d.dispDate();
			cout << ", ";
			t.dispTime();
			cout << ")\n";
		}
		
		void readInput()
		{
			cout << "Title: ";
			cin.ignore();
			getline(cin, title);
			cout << "Hall: ";
			cin >> hallNo;
			cout << "Ticket price: RM";
			cin >> price;
			cout << "Date [dd-mm-yyyy]: ";
			d.readDate();
			cout << "Time [hour:min] using 24-hour format: ";
			t.readTime();
		}
		
		void displayInfoMovie()
		{
			cout << setw(7) << "Title" << ": " << title << endl;
			cout << setw(7) << "Hall" << ": " << hallNo << endl;
			cout << setw(7) << "Date" << ": ";
					d.dispDate();
			cout << endl;
			cout << setw(7) << "Time" << ": ";
					t.dispTime();
			cout << endl;
			cout << setw(7) << "Price" << ": RM" << fixed << setprecision(2) << price << endl; 
		}
		
		void displayInfoAudience()
		{
			if (numAudience == 0)
			{
				cout << "No audience!" << endl;
			}
			else
			{
				cout << setw(18) << "Number of Audience" << ": " << numAudience << endl
					 << setw(18) << "Number of Adults" << ": " << numAdults << endl
					 << setw(18) << "Number of Kids" << ": " << numKids << endl;
					 
				cout << endl 
					 << "No" << setw(20) << "Name" << setw(8) << "Age" 
					 << setw(20) << "Parent Name" << setw(8) << "Seat No." << "Ticket (RM)" << endl;					
				
				double total = 0;
				
				for (int i=0; i<numAudience; ++i)
				{
					cout << setw(4) << i+1;
					audienceList[i]->displayDetails();
					price = price * (audienceList[i]->calcDisc());
					cout << fixed << setprecision(2) << price << endl;
					total += price;
				}	 		
				
				cout << "\nTotal ticket price = RM" << fixed << setprecision(2) << total << endl;		
			}
		}
};

int displayMenu()
{
	int choice;
	cout << "========== Menu ==========\n" 
		 << "[1] Add Movie" << endl
		 << "[2] Add Audience" << endl
		 << "[3] Display Movies" << endl
		 << "[4] Display Audiences" << endl
		 << "[5] Exit" << endl
		 << "==========================\n\n" 
		 << "Select task: ";
	cin >> choice;

	return choice;
}
 

int main()
{
	int choice;
	Movie movie[10];	
	int count = 0;
	
	choice = displayMenu();
	
	while(choice >=1 && choice <= 5)
	{
		switch(choice)
		{
			case 1:
			{
				for(int i=count; i<=count; ++i)
				{
					cout << "\n<<< Add Movie >>>" << endl;
					movie[i].readInput();
				}
				count++;
				break;
			}
	
			case 2:
			{
	 			
				char choice2 = 'y';
				int choice3;
				cout << "\n<<< Add Audience >>>" << endl;
				cout << endl;
				cout << "Movie List" << endl;
				
				for(int i=0; i<count; ++i)
				{
					for(int j=0; j<count; ++j)
					{	
						cout << j+1 << ") " << movie[j].getTitle();
						movie[j].getDateTime();
					}
					
					cout << "\nSelect movie: ";
					cin >> choice3;
					
					cout << endl
						 << "--- Enter Audience Info ---" << endl;
					
					string name, pname;
					int age = 0, seatNo = 0;
					
					while (choice2 == 'y')
					{
						cout << left;
						cout << setw(7) << "Name" << ": ";
						cin.ignore();
						getline(cin, name);
						cout << setw(7) << "Age" << ": ";
						cin >> age;
						if(age > 12)
						{
							cout << setw(7) << "Seat No" << ": ";
							cin >> seatNo;
							
							if(movie[choice3+1].getSeat(seatNo) == false)
							{
								cout << "Seat unavailable, choose others!" << endl << endl;
								cin >> seatNo;
							}
							
							movie[i].addAudience(new Adults(name, age, seatNo));
						}
						else
						{
							cout << "Parent Name: ";
							cin >> pname;
							
							movie[i].addAudience(new Kids (name, age, seatNo, pname));
						}
						
						cout << endl
							 << "Press 'Y' to continue >> ";
						cin >> choice2;
						cout << endl;
					}	
				}
				break;
			}
			
			case 3:
			{
				if (count == 0)
				{
					cout << "Sorry!! No movie data to display..." << endl;
				}
				else
				{
					cout << "\n<<< Movie Info >>>" << endl;
					cout << endl 
						 << "Number of Movies: " << count << endl;
					
					for(int i=0; i<count; ++i)
					{
						cout << endl
							 << "Movie #" << i+1 << endl;
						movie[i].displayInfoMovie();
					}
				}
				break;
			}
			
			case 4:
			{
				if (count == 0)
				{
					cout << "Sorry!! No movie, please add movie first..." << endl;
				}
				else
				{
					cout << "\n<<< Movie(s) and Audience(s) Info >>>" << endl;
					cout << endl 
						 << "Number of Movies: " << count << endl;
					
					for(int i=0; i<count; ++i)
					{
						cout << endl
							 << "Movie #" << i+1 << endl;
						movie[i].displayInfoMovie();
						cout << endl;
						movie[i].displayInfoAudience();
					}
				}
				break;
			}
			
			default:
			{
				cout << "Thank you! :)" << endl;
				exit(0);
			}
		}
		cout << endl;
		choice = displayMenu();
	}
	
	return 0;
}

Movie class now needs a destructor to free the allocated memory. See mine above.

Also see the changes I made to the classes....
yes, I am using dev cpp, and I also add on the destructor, but it also terminates.
and for the seat also, I don't know why it cant function
Now is the time to become good friends with the debugger. Using the debugger, trace through the code to make sure it's behaving as expected and that the variables contain the expected values. When you find that which isn't as expected - then you're found a problem.
solved! thank you so much
Topic archived. No new replies allowed.