Introduction
In this tutorial, we will create a simple Movie Database Management System using C++. This system will allow users to store, display, and manage data related to movies such as title, release year, director, and genre. The objective of this program is to help you understand basic file handling, object-oriented programming, and how to manage a small database using C++.
Objective
The main goal is to build a basic system that allows for the addition of movie information, displaying stored movies, and simple management operations like viewing or removing movies. It will also give you a chance to practice C++ classes and file input/output operations.
Program Code
#include #include #include #include using namespace std; // Movie class to hold movie details class Movie { public: string title; int year; string director; string genre; // Constructor to initialize a movie Movie(string t, int y, string d, string g) : title(t), year(y), director(d), genre(g) {} // Function to display movie details void displayMovie() { cout << "Title: " << title << "\n"; cout << "Year: " << year << "\n"; cout << "Director: " << director << "\n"; cout << "Genre: " << genre << "\n"; } // Function to save movie to file void saveToFile(ofstream &outFile) { outFile << title << "," << year << "," << director << "," << genre << endl; } }; // Function to read all movies from the file void loadMovies(vector& movieList) { ifstream inFile("movies.txt"); string title, director, genre; int year; while (getline(inFile, title, ',') && inFile >> year && getline(inFile, director, ',') && getline(inFile, genre)) { movieList.push_back(Movie(title, year, director, genre)); inFile.ignore(); // Ignore the newline character } inFile.close(); } // Function to add a movie to the file void addMovie() { string title, director, genre; int year; cout << "Enter movie title: "; cin.ignore(); getline(cin, title); cout << "Enter release year: "; cin >> year; cin.ignore(); cout << "Enter director: "; getline(cin, director); cout << "Enter genre: "; getline(cin, genre); ofstream outFile("movies.txt", ios::app); Movie newMovie(title, year, director, genre); newMovie.saveToFile(outFile); outFile.close(); cout << "Movie added successfully!\n"; } // Function to display all movies void displayMovies(const vector& movieList) { if (movieList.empty()) { cout << "No movies found.\n"; return; } for (const auto& movie : movieList) { movie.displayMovie(); cout << "----------------------\n"; } } int main() { vector movieList; // Load existing movies from file loadMovies(movieList); int choice; do { cout << "Movie Database Menu:\n"; cout << "1. Add Movie\n"; cout << "2. Display Movies\n"; cout << "3. Exit\n"; cout << "Enter your choice: "; cin >> choice; switch (choice) { case 1: addMovie(); break; case 2: displayMovies(movieList); break; case 3: cout << "Exiting the program.\n"; break; default: cout << "Invalid choice, please try again.\n"; break; } } while (choice != 3); return 0; }
Program Explanation
This program allows you to manage a small movie database. The program structure is broken down as follows:
- Movie Class: This class holds the attributes of a movie, such as title, year, director, and genre. It includes methods to display the movie details and save it to a file.
- File Handling: Movies are saved to a text file (“movies.txt”) using simple file input/output operations. The program reads from the file at startup to load existing movie data and appends new movie data when a new movie is added.
- Menu System: The main function offers a simple menu system that allows users to add movies, view the list of movies, or exit the program.
How to Run the Program
- Copy the code into a text file and save it with a .cpp extension (e.g., MovieDatabase.cpp).
- Compile the program using a C++ compiler. For example, using g++:
g++ MovieDatabase.cpp -o MovieDatabase
- Run the compiled executable:
./MovieDatabase
- The program will prompt you to add new movies, view existing movies, or exit the application.