Prj Hlp

closed account (3RGNvqkS)
Hello This is the code that I've addressed last time and I made a new one and made a new code and currently working on it. This is my concern When you input 6 in the movie menu, to check Movie Availability, you will proceed to Check Movie code. However, when you input a Movie code which is unavailable, instead of telling the program, Movie is unavailable(line 217), it will tell the system it will just exit the program instead of going back to Movie Menu. Can someone help me fix the code based on the link below. I just put a link there since the code is too long.

Last edited on
The reason for the crash is likely on line 206. If there is no movie for forRentCustMov the foundMovie will be invalid (ideally nullptr). When you access this invalid pointer on line 206 the program will crash.

So you need to break the loop and test foundMovie:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
  while (foundMovie -> movl.movCode != forRentCustMov) {
    foundMovie = foundMovie -> Mnext;
if(! foundMovie)
  break;
  }
  if (foundMovie -> movl.movCode == forRentCustMov) { // Note: When 'foundMovie != nullptr' 'foundMovie -> movl.movCode == forRentCustMov' is also fulfilled 
    if (foundMovie -> movl.movCopies != 0) {
      cout << "Movie is available! " << endl;
      cout << "Movie Title: " << foundMovie -> movl.movTitle << endl;
      cout << "Genre: " << foundMovie -> movl.movGenre << endl;
      cout << "Production: " << foundMovie -> movl.movproduction << endl;
      cout << "Number of Copies: " << foundMovie -> movl.movCopies << endl << endl;
    } else {
      cout << "Movie is unavailable " << endl;
    }
  } else {
      cout << "Movie is unavailable " << endl;
  }
  foundMovie = foundMovie -> Mnext;
As a first refactor, consider something like (NOT tried) [posted in 3 posts due to code size limitations]

Part 1:

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
#include <string>
#include <deque>
#include <stack>
#include <sstream>
#include <fstream>
#include <iostream>

using namespace std;

struct Movie {
	int movCode {};
	std::string movTitle;
	std::string movGenre;
	std::string movproduction;
	int movCopies {};
};

struct Customer {
	int custCode {};
	std::string custName;
	std::string custAddress;
	stack < int > custRentmov;
};

struct RentByCustomer {
	int rentMovieCode {};
	std::string rentMovName;
	int movCopies2 {};
	int rentCustomerID {};
	std::string rentCustName;
};

class movieList {
private:
	Customer custlq;
	bool cQcheck {};
	deque < Customer > custQueue;
	RentByCustomer rentMovbyC;
	stack < RentByCustomer > custMova;
	stack < RentByCustomer > custMovb;
	stack < int > custMovc;

	struct movieListNode {
		Movie movl;
		struct movieListNode* Mnext {};
	};

	movieListNode* Moviehead {};

public:
	movieList() {}
	~movieList();

	movieList(const movieList&) = delete;
	movieList& operator=(const movieList&) = delete;

	void addMovie();
	void rentMovie(int movCode, int rentCustomerID);
	void returnMovie(int rentByCustomer, int returnedMovie);
	bool isCustomerEmpty() const ;
	void SaveCustRentMov();
	void rentedByCustomer();
	void showMovieDetails(int movCode) const;
	void displayMovies() const;
	void movieAvailability(int movCode) const;
	void OpenMovieFile();
	void SaveMovieFile();
	void addCustomer();
	void custDets(int) const ;
	void rentListCustomer(int);
	void OpenCustomerFile();
	void SaveCustomerFile();
};

movieList::~movieList() {
	for (; Moviehead; ) {
		const auto ptr { Moviehead->Mnext };

		delete Moviehead;
		Moviehead = ptr;
	}
}

void movieList::displayMovies() const {
	cout << "Display All Movies\n";
	cout << "Movies Available:\n\n";

	for (auto MovieFile { Moviehead }; MovieFile; MovieFile = MovieFile->Mnext) {
		cout << "--------------------------------\n";
		cout << "Movie Code: " << MovieFile->movl.movCode << '\n';
		cout << "Movie Title: " << MovieFile->movl.movTitle << '\n';
		cout << "Movie Genre: " << MovieFile->movl.movGenre << '\n';
		cout << "Movie Producer: " << MovieFile->movl.movproduction << '\n';
		cout << "Number of Copies: " << MovieFile->movl.movCopies << "\n\n";
		cout << "--------------------------------\n";
	}
}

void movieList::addCustomer() {
	cQcheck = true;

	cout << "Enter the Customer Code: ";
	cin >> custlq.custCode;
	cin.ignore();

	cout << "Enter the Customer Name: ";
	getline(cin, custlq.custName);

	cout << "Enter the Customer Address: ";
	getline(cin, custlq.custAddress);

	custQueue.push_back(custlq);
	cout << "\nGood Job! You have added a customer!\n";
	//cout << "Press any key.\n";
}

void movieList::custDets(int forShowCustDetails) const {
	if (!custQueue.empty()) {
		for (auto& cust : custQueue)
			if (cust.custCode == forShowCustDetails) {
				cout << "Customer Name: " << cust.custName << '\n';
				cout << "Address: " << cust.custAddress << '\n';
				break;
			}
	} else
		cout << "Customer is empty!\n";

	//cout << "Press any key.\n";
}

void movieList::addMovie() {
	auto movnewNode { new movieListNode };

	cout << "Enter the Movie Code ";
	cin >> movnewNode->movl.movCode;
	cin.ignore();

	cout << "Enter the Movie title: ";
	getline(cin, movnewNode->movl.movTitle);

	cout << "Enter the Movie genre: ";
	getline(cin, movnewNode->movl.movGenre);

	cout << "Enter the Movie producer: ";
	getline(cin, movnewNode->movl.movproduction);

	cout << "Enter the number of copies: ";
	cin >> movnewNode->movl.movCopies;
	cin.ignore();
	cout << '\n';

	if (!Moviehead)
		Moviehead = movnewNode;
	else {
		auto movnodePtr { Moviehead };

		for (; movnodePtr->Mnext; movnodePtr = movnodePtr->Mnext);
		movnodePtr->Mnext = movnewNode;
	}

	ofstream smf("Movies.txt", ios::app);

	if (smf.is_open()) {
		smf << movnewNode->movl.movCode << '\n';
		smf << movnewNode->movl.movTitle << '\n';
		smf << movnewNode->movl.movGenre << '\n';
		smf << movnewNode->movl.movproduction << '\n';
		smf << movnewNode->movl.movCopies << '\n';
	}

	cout << "\nYou have successfully added the movie!\n";
	//cout << endl << "Press any key.\n";
}

void movieList::rentMovie(int movCode, int rentCustomerID) {
	auto itr { custQueue.begin() };

	for (; itr != custQueue.end(); ++itr)
		if (itr->custCode == rentCustomerID)
			break;

	if (itr == custQueue.end()) {
		cout << "Customer id not found\n";
		return;
	}

	for (auto movnodePtr { Moviehead }; movnodePtr; movnodePtr = movnodePtr->Mnext)
		if (movnodePtr->movl.movCode == movCode) {
			movnodePtr->movl.movCopies -= 1;
			rentMovbyC.movCopies2 = 0;
			rentMovbyC.rentMovieCode = movnodePtr->movl.movCode;
			rentMovbyC.rentMovName = movnodePtr->movl.movTitle;
			rentMovbyC.rentCustomerID = itr->custCode;
			rentMovbyC.rentCustName = itr->custName;
			++rentMovbyC.movCopies2;
			custMova.push(rentMovbyC);
			cout << "You have successfully rented the movie\n";
			//cout << "Press any key.\n";
			return;
		}

	cout << "Movie code not found\n";
}

void movieList::movieAvailability(int forRentCustMov) const {
	auto foundMovie {Moviehead};

	for (; foundMovie && foundMovie->movl.movCode != forRentCustMov; foundMovie = foundMovie->Mnext);

	if (foundMovie && foundMovie->movl.movCode == forRentCustMov)
		if (foundMovie->movl.movCopies != 0) {
			cout << "Movie is available!\n";
			cout << "Movie Title: " << foundMovie->movl.movTitle << '\n';
			cout << "Genre: " << foundMovie->movl.movGenre << '\n';
			cout << "Production: " << foundMovie->movl.movproduction << '\n';
			cout << "Number of Copies: " << foundMovie->movl.movCopies << "\n\n";
		} else
			cout << "Movie is unavailable - No copies\n";
	else
		cout << "Movie not found\n ";
}

void movieList::showMovieDetails(int forRentCustMov) const {
	bool found {};

	for (auto movDetailshow { Moviehead }; movDetailshow; movDetailshow = movDetailshow->Mnext)
		if (movDetailshow->movl.movCode == forRentCustMov) {
			found = true;
			cout << "Movie Title: " << movDetailshow->movl.movTitle << '\n';
			cout << "Genre: " << movDetailshow->movl.movGenre << '\n';
			cout << "Production: " << movDetailshow->movl.movproduction << '\n';
			cout << "Number of Copies: " << movDetailshow->movl.movCopies << "\n\n";
		}

	if (!found)
		cout << "Movie Code is not on the list!\n";
}

void movieList::rentListCustomer(int forListRentByCustomer) {
	movieListNode* movnodePtr { Moviehead };

	for (auto itr { custQueue.begin() }; itr != custQueue.end(); ++itr) {
		if (itr->custCode == forListRentByCustomer) {
			cout << "Customer Name: " << itr->custName << '\n';
			cout << "Customer Address: " << itr->custAddress << '\n';
			break;
		}
	}

	if (custMova.empty())
		cout << endl << "\nThe Customer's list is empty!\n";

	while (!custMova.empty()) {
		rentMovbyC = custMova.top();

		if (rentMovbyC.rentCustomerID == forListRentByCustomer)
			if (rentMovbyC.movCopies2 > 0) {
				cout << "Movies Rented by the customer:\n\n";
				cout << "Movie Code: " << rentMovbyC.rentMovieCode << '\n';

				while (movnodePtr)
					if (movnodePtr->movl.movCode == rentMovbyC.rentMovieCode) {
						rentMovbyC.rentMovName = movnodePtr->movl.movTitle;
						break;
					} else
						movnodePtr = movnodePtr->Mnext;

				cout << "Movie Title: " << rentMovbyC.rentMovName << "\n\n";
				custMovb.push(rentMovbyC);
				custMova.pop();
			} else
				cout << "List of Movies Rented is empty!\n";
		else {
			custMovb.push(rentMovbyC);
			custMova.pop();
		}
	}

	while (!custMovb.empty()) {
		rentMovbyC = custMovb.top();
		custMovb.pop();
		custMova.push(rentMovbyC);
	}
}

Last edited on
Part 2:

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
void movieList::returnMovie(int rentByCustomer, int movReturnCust) {
	if (custMova.empty()) {
		cout << "Customer has not rented yet...\n";
		return;
	}

	for (auto returnPtr { Moviehead }; returnPtr; returnPtr = returnPtr->Mnext)
		if (returnPtr->movl.movCode == movReturnCust) {
			++returnPtr->movl.movCopies;
			break;
		}

	while (!custMova.empty()) {
		rentMovbyC = custMova.top();

		if (rentMovbyC.rentCustomerID == rentByCustomer && rentMovbyC.rentMovieCode == movReturnCust) {
			--rentMovbyC.movCopies2;
			custMova.pop();

			if (custMova.empty()) {
				cout << "Movie has been returned successfully!\n";
				break;
			}
		}

		custMovb.push(rentMovbyC);
		custMova.pop();
	}

	while (!custMovb.empty()) {
		rentMovbyC = custMovb.top();
		custMova.push(rentMovbyC);
		custMovb.pop();
	}

	cout << "Press any key.\n";
}

void movieList::OpenMovieFile() {
	ifstream openMov("Movies.txt");
	auto openFilePtr { Moviehead };

	std::string readData;
	std::string movdata1;
	std::string movdata2;
	std::string movdata3;
	std::string movdata4;
	stringstream open1;
	stringstream open2;
	int mov_Code {};
	int mov_Copies {};
	int c {};

	while (getline(openMov, readData)) {
		if (c == 0) {
			open1 << readData;
			open1 >> mov_Code;
			++c;
		} else if (c == 1) {
			movdata1 = readData;
			++c;
		} else if (c == 2) {
			movdata2 = readData;
			++c;
		} else if (c == 3) {
			movdata3 = readData;
			++c;
		} else if (c == 4) {
			open2 << readData;
			open2 >> mov_Copies;
			++c;
		} else {
			auto movnewNode { new movieListNode };

			movdata4 = readData;
			movnewNode->movl.movCode = mov_Code;
			movnewNode->movl.movTitle = movdata1;
			movnewNode->movl.movGenre = movdata2;
			movnewNode->movl.movproduction = movdata3;
			movnewNode->movl.movCopies = mov_Copies;
			c = 0;
			movnewNode->Mnext = nullptr;

			if (!Moviehead) {
				Moviehead = movnewNode;
				openFilePtr = Moviehead;
			} else {
				openFilePtr->Mnext = movnewNode;
				openFilePtr = openFilePtr->Mnext;
			}
		}

		open1.clear();
		open2.clear();
	}
}

void movieList::OpenCustomerFile() {
	int custCurr {};
	ifstream openCust("Customers.txt");

	while (openCust) {
		if (cQcheck == true)
			break;

		openCust >> custlq.custCode;
		openCust.ignore();

		if (custlq.custCode == custCurr)
			break;

		custCurr = custlq.custCode;
		getline(openCust, custlq.custName);
		getline(openCust, custlq.custAddress);
		openCust.ignore();
		custQueue.push_back(custlq);
	}

	cQcheck = true;
}

void movieList::rentedByCustomer() {
	ifstream openRentMovC("RentedByCustomer.txt");

	if (openRentMovC) {
		int RentCID {};

		while (openRentMovC >> RentCID) {
			int RentMID {};

			rentMovbyC.rentCustomerID = RentCID;
			openRentMovC >> RentMID;
			rentMovbyC.rentMovieCode = RentMID;
			custMovc.push(RentMID);
			rentMovbyC.movCopies2 += 1;
			custMova.push(rentMovbyC);
		}
	}
}

void movieList::SaveMovieFile() {
	ofstream mdata("Movies.txt");

	if (mdata)
		for (auto saveMfile { Moviehead }; saveMfile; saveMfile = saveMfile->Mnext) {
			mdata << saveMfile->movl.movCode << '\n';
			mdata << saveMfile->movl.movTitle << '\n';
			mdata << saveMfile->movl.movGenre << '\n';
			mdata << saveMfile->movl.movproduction << '\n';
			mdata << saveMfile->movl.movCopies << '\n';
		}
}

void movieList::SaveCustomerFile() {
	ofstream cdata("Customers.txt");

	if (cdata)
		for (const auto& c : custQueue) {
			cdata << c.custCode << '\n';
			cdata << c.custName << '\n';
			cdata << c.custAddress << '\n';
		}
}

void movieList::SaveCustRentMov() {
	ofstream rentData("RentedByCustomer.txt", ios::trunc);

	for (; !custMova.empty(); custMova.pop() ) {
		rentMovbyC = custMova.top();
		rentData << rentMovbyC.rentCustomerID << '\n';
		rentData << rentMovbyC.rentMovieCode << '\n';
		rentData << '\n';
	}
}

bool movieList::isCustomerEmpty() const {
	if (custQueue.empty()) {
		cout << "Invalid Customer ID!\n";
		return true;
	} else
		return false;
}

int menu() {
	int choice {};

	cout << "Movie Menu\n";
	cout << "[1] Insert a New Movie\n";
	cout << "[2] Rent a Movie\n";
	cout << "[3] Return a Movie\n";
	cout << "[4] Show Movie Details\n";
	cout << "[5] Display All Movie\n";
	cout << "[6] Check Movie Availability\n";
	cout << "[7] Customer's Maintainance\n";
	cout << "[8] Exit Program\n\n";
	cout << "Enter choice: ";
	cin >> choice;
	return choice;
}

Part 3:

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
int main() {
	movieList movie;

	movie.OpenMovieFile();
	movie.OpenCustomerFile();
	movie.rentedByCustomer();

	while (true) {
		const int choice { menu() };

		cout << "Enter your choice: ";

		switch (choice) {
			case 1:
				cout << "Insert A New Movie\n";
				movie.addMovie();
				break;

			case 2:
				{
					int rentByCustomer {}, rentMovie {};

					cout << "Rent A Movie\n";
					cout << "Enter Customer Code: ";
					cin >> rentByCustomer;

					if (!movie.isCustomerEmpty()) {
						cout << "Enter Movie Code to Rent: ";
						cin >> rentMovie;

						movie.rentMovie(rentMovie, rentByCustomer);
					}
				}
				break;

			case 3:
				{
					int rentByCustomer {}, returnedMovie {};

					cout << "Return A Movie\n";
					cout << "Enter Customer Code: ";
					cin >> rentByCustomer;

					if (!movie.isCustomerEmpty()) {
						cout << "Enter Movie Code to return: ";
						cin >> returnedMovie;
						movie.returnMovie(rentByCustomer, returnedMovie);
					}
				}
				break;

			case 4:
				{
					int rentMovie {};

					cout << "Show Movie Details\n";
					cout << "Enter Movie Code: ";
					cin >> rentMovie;
					cout << '\n';

					movie.showMovieDetails(rentMovie);
					//cout << "Press any key." << endl;
				}
				break;

			case 5:
				movie.displayMovies();
				break;

			case 6:
				{
					int rentMovie {};

					cout << "Check Movie Availability\n";
					cout << "Enter a Movie Code: ";
					cin >> rentMovie;
					movie.movieAvailability(rentMovie);
					//cout << "Press any key." << endl;
				}
				break;

			case 7:
				cout << "Customer Maintenance\n";
				cout << "[1] Add New Customer\n";
				cout << "[2] Show Customer Details\n";
				cout << "[3] List All Movies Rented by a Customer\n";

				{
					int ch1 {}, customerDetails {}, rentList {};
					cout << "Enter choice: ";
					cin >> ch1;

					switch (ch1) {
						case 1:
							cout << "Add New Customer\n";
							movie.addCustomer();
							break;

						case 2:
							cout << "Show Customer Details\n";
							cout << "Enter a Customer Code: ";
							cin >> customerDetails;

							movie.custDets(customerDetails);
							break;

						case 3:
							cout << "List All Movies Rented By The Customer\n";
							cout << "Enter a Customer ID: ";
							cin >> rentList;

							movie.rentListCustomer(rentList);
							//cout << "Press any key." << endl;
							break;

						default:
							cout << "Try Again.";
							break;
					}
				}
				break;

			case 8:
				movie.SaveMovieFile();
				movie.SaveCustomerFile();
				movie.SaveCustRentMov();
				cout << "Thank You! Please Come Again\n";
				return 0;

			default:
				cout << "Try Again.";
				break;
		}
	}
}

Topic archived. No new replies allowed.