Seg fault for c++ program

Whenever I try to enter the name of the description of a movie, or anything else related to a string entry, it always seg faults. I can enter the name and genre of a movie fine.

#include "splashkit.h"
#include <string>

using namespace std;
using std::stoi;
using std::to_string;

struct Review {
    string description;
    int rating;
};

struct Movie {
    string name;
    string genre;
    string description;
    Review reviews[100];
    int review_count;
};

// Database struct
struct Database {
    Movie movies[100]; // Array to hold movies, assuming max 100 movies
    string genres[100]; // Array to hold genres, assuming max 100 genres
    int ratings[100];
    int movie_count;
    int genre_count;
    int rating_count;
};

int read_integer(const string& prompt) {
    write(prompt);
    return stoi(read_line());
}

void add_movie(Database &db);
void view_all_movies(Database &db);
void view_details(Database &db, int movie_index);
void alter_movie(Database &db, int movie_index);
void add_review(Database &db, int movie_index);
void delete_movie(Database &db, int movie_index);
void list_movies_with_genre(Database &db);
void list_all_movies_above_desired_rating(Database &db);
void main_menu(Database &db);
void details_menu(Database &db, int movie_index);

// Function to add a new movie to the database
void add_movie(Database &db) {
    Movie new_movie;

    // Get user input for movie details
    write_line("Adding a New Movie:");
    write_line("Enter the name of the movie:");
    new_movie.name = read_line();

    write_line("Select the genre of the movie:");
    write_line("1. Action");
    write_line("2. Romance");
    write_line("3. Sci-Fi");
    write_line("4. Fantasy");
    int genre_choice;
    do {
        write_line("Enter your choice (1-4):");
        genre_choice = stoi(read_line());
        switch (genre_choice) {
            case 1:
                new_movie.genre = "Action";
                break;
            case 2:
                new_movie.genre = "Romance";
                break;
            case 3:
                new_movie.genre = "Sci-Fi";
                break;
            case 4:
                new_movie.genre = "Fantasy";
                break;
            default:
                write_line("Invalid choice. Please enter a number between 1 and 4.");
                break;
        }
    } while (genre_choice < 1 || genre_choice > 4);

    // Initialize description to an empty string
    new_movie.description = "";

    write_line("Enter the description of the movie:");
    new_movie.description = read_line(); // Store the description

    // Store the new movie in the database
    db.movies[db.movie_count++] = new_movie;
}

// Function to view all movies and select one for action
void view_all_movies(Database &db) {
    write_line("Movies in the Database:");
    if (db.movie_count == 0) {
        write_line("No movies found.");
    } else {
        for (int i = 0; i < db.movie_count; ++i) {
            write_line(to_string(i + 1) + ": " + db.movies[i].name);
        }
        write_line("Enter the number of the movie you would like to select:");
        int movie_number;
        do {
            movie_number = stoi(read_line());
            if (movie_number < 1 || movie_number > db.movie_count) {
                write_line("Invalid movie number. Please enter a number between 1 and " + to_string(db.movie_count) + ":");
            }
        } while (movie_number < 1 || movie_number > db.movie_count);
        
        // Pass control to the details_menu for the selected movie
        details_menu(db, movie_number - 1);
    }
    // Return to the main menu after details_menu
    main_menu(db); // Update function call
}

void details_menu(Database &db, int movie_index) {
    Movie selected_movie = db.movies[movie_index];
    write_line("Details Menu for " + selected_movie.name + ":");
    write_line("1. View Details");
    write_line("2. Alter Movie");
    write_line("3. Add Review");
    write_line("4. Delete Movie");
    write_line("Enter your choice (1-4):");
    int choice;
    do {
        choice = stoi(read_line());
        switch (choice) {
            case 1:
                view_details(db, movie_index);
                break;
            case 2:
                alter_movie(db, movie_index);
                break;
            case 3:
                // Add Review
                add_review(db, movie_index);
                break;
            case 4:
                // Delete Movie
                delete_movie(db, movie_index);
                break;
            default:
                write_line("Invalid choice. Please enter a number between 1 and 4.");
                break;
        }
    } while (choice < 1 || choice > 4);
}

void main_menu(Database &db) {
    int choice = 0;
    do {
        write_line("Main Menu:");
        write_line("1. Add Movie");
        write_line("2. View All Movies");
        write_line("3. List Movies with Genre");
        write_line("4. List Movies above Desired Rating");
        write_line("5. Main Menu");
        write_line("Enter your choice (1-5): ");
        choice = stoi(read_line());
        switch (choice) {
            case 1:
                add_movie(db);
                break;
            case 2:
                view_all_movies(db);
                break;
            case 3:
                list_movies_with_genre(db); // Call the function to list movies with genre
                break;
            case 4:
                list_all_movies_above_desired_rating(db); // Call the function to list movies above desired rating
                break;
            case 5:
                // Main Menu (do nothing, loop will continue)
                break;
            default:
                write_line("Invalid choice. Please enter a number between 1 and 5.");
                break;
        }
    } while (choice != 5);
}

void view_details(Database &db, int movie_index) {
    Movie selected_movie = db.movies[movie_index];
    write_line("Details for " + selected_movie.name + ":");
    write_line("Genre: " + selected_movie.genre);
    write_line("Description: " + selected_movie.description);
    write_line("Reviews:");
    if (selected_movie.review_count == 0) {
        write_line("No reviews available.");
    } else {
        for (int i = 0; i < selected_movie.review_count; ++i) {
            write_line("Review " + to_string(i + 1) + ": " + selected_movie.reviews[i].description);
            write_line("Rating: " + to_string(selected_movie.reviews[i].rating));
        }
    }
}

void alter_movie(Database &db, int movie_index) {
    Movie &selected_movie = db.movies[movie_index]; // Reference to the selected movie
    write_line("Select what you want to alter:");
    write_line("1. Name");
    write_line("2. Genre");
    write_line("3. Description");
    write_line("4. Review Description");
    write_line("5. Rating");
    write_line("Enter your choice (1-5):");
    int choice;
    do {
        choice = stoi(read_line());
        switch (choice) {
            case 1:
                write_line("Enter the new name:");
                selected_movie.name = read_line();
                break;
            case 2:
                write_line("Select the new genre:");
                write_line("1. Action");
                write_line("2. Romance");
                write_line("3. Sci-Fi");
                write_line("4. Fantasy");
                int genre_choice;
                do {
                    write_line("Enter your choice (1-4):");
                    genre_choice = stoi(read_line());
                    switch (genre_choice) {
                        case 1:
                            selected_movie.genre = "Action";
                            break;
                        case 2:
                            selected_movie.genre = "Romance";
                            break;
                        case 3:
                            selected_movie.genre = "Sci-Fi";
                            break;
                        case 4:
                            selected_movie.genre = "Fantasy";
                            break;
                        default:
                            write_line("Invalid choice. Please enter a number between 1 and 4.");
                            break;
                    }
                } while (genre_choice < 1 || genre_choice > 4);
                break;
            case 3:
                write_line("Enter the new description:");
                selected_movie.description = read_line();
                break;
            case 4:
                if (selected_movie.review_count == 0) {
                    write_line("No reviews available to alter.");
                } else {
                    write_line("Select the review to alter:");
                    for (int i = 0; i < selected_movie.review_count; ++i) {
                        write_line(to_string(i + 1) + ". " + selected_movie.reviews[i].description);
                    }
                    int review_choice;
                    do {
                        write_line("Enter your choice (1-" + to_string(selected_movie.review_count) + "):");
                        review_choice = stoi(read_line());
                        if (review_choice < 1 || review_choice > selected_movie.review_count) {
                            write_line("Invalid choice. Please enter a number between 1 and " + to_string(selected_movie.review_count) + ".");
                        }
                    } while (review_choice < 1 || review_choice > selected_movie.review_count);
                    write_line("Enter the new review description:");
                    selected_movie.reviews[review_choice - 1].description = read_line();
                }
                break;
            case 5:
                if (selected_movie.review_count == 0) {
                    write_line("No reviews available to alter.");
                } else {
                    write_line("Select the review to alter:");
                    for (int i = 0; i < selected_movie.review_count; ++i) {
                        write_line(to_string(i + 1) + ". " + selected_movie.reviews[i].description);
                    }
                    int review_choice;
                    do {
                        write_line("Enter your choice (1-" + to_string(selected_movie.review_count) + "):");
                        review_choice = stoi(read_line());
                        if (review_choice < 1 || review_choice > selected_movie.review_count) {
                            write_line("Invalid choice. Please enter a number between 1 and " + to_string(selected_movie.review_count) + ".");
                        }
                    } while (review_choice < 1 || review_choice > selected_movie.review_count);
                    write_line("Enter the new rating:");
                    selected_movie.reviews[review_choice - 1].rating = stoi(read_line());
                }
                break;
            default:
                write_line("Invalid choice. Please enter a number between 1 and 5.");
                break;
        }
    } while (choice < 1 || choice > 5);
}

void add_review(Database &db, int movie_index) {
    Movie &selected_movie = db.movies[movie_index]; // Reference to the selected movie
    if (selected_movie.review_count >= 10) {
        write_line("Sorry, you can't add more than 10 reviews for a movie.");
        return;
    }
    write_line("Write a description about how you felt about the movie:");
    string review_description = read_line();
    write_line("Rate the movie from 1 to 10:");
    int rating;
    do {
        rating = stoi(read_line());
        if (rating < 1 || rating > 10) {
            write_line("Invalid rating. Please enter a number between 1 and 10:");
        }
    } while (rating < 1 || rating > 10);

    // Add the review to the selected movie
    selected_movie.reviews[selected_movie.review_count].description = review_description;
    selected_movie.reviews[selected_movie.review_count].rating = rating;
    selected_movie.review_count++;

    write_line("Review added successfully!");
}

void delete_movie(Database &db, int movie_index) {
    Movie &selected_movie = db.movies[movie_index]; // Reference to the selected movie

    // Shift movies to the left to fill the gap caused by deletion
    for (int i = movie_index; i < db.movie_count - 1; ++i) {
        db.movies[i] = db.movies[i + 1];
    }

    // Decrease the movie count to reflect the deletion
    db.movie_count--;

    write_line("Movie '" + selected_movie.name + "' deleted successfully.");
}

void list_movies_with_genre(Database &db) {
    write_line("Select the genre of movies you would like to see:");
    write_line("1. Action");
    write_line("2. Romance");
    write_line("3. Sci-Fi");
    write_line("4. Fantasy");
    
    int genre_choice;
    do {
        write_line("Enter your choice (1-4):");
        genre_choice = stoi(read_line());
        if (genre_choice < 1 || genre_choice > 4) {
            write_line("Invalid choice. Please enter a number between 1 and 4.");
        }
    } while (genre_choice < 1 || genre_choice > 4);

    string selected_genre;
    switch (genre_choice) {
        case 1:
            selected_genre = "Action";
            break;
        case 2:
            selected_genre = "Romance";
            break;
        case 3:
            selected_genre = "Sci-Fi";
            break;
        case 4:
            selected_genre = "Fantasy";
            break;
    }

    bool found = false;
    write_line("Movies with Genre '" + selected_genre + "':");
    for (int i = 0; i < db.movie_count; ++i) {
        if (db.movies[i].genre == selected_genre) {
            write_line(db.movies[i].name);
            found = true;
        }
    }
    if (!found) {
        write_line("No movies found with genre '" + selected_genre + "'.");
    }
}

void list_all_movies_above_desired_rating(Database &db) {
    write_line("Enter the desired rating (1-10):");
    int desired_rating;
    do {
        desired_rating = stoi(read_line());
        if (desired_rating < 1 || desired_rating > 10) {
            write_line("Invalid rating. Please enter a number between 1 and 10:");
        }
    } while (desired_rating < 1 || desired_rating > 10);

    bool found = false;
    write_line("Movies with Rating Above or Equal to " + to_string(desired_rating) + ":");
    for (int i = 0; i < db.movie_count; ++i) {
        bool rating_above_desired = false;
        for (int j = 0; j < db.movies[i].review_count; ++j) {
            if (db.movies[i].reviews[j].rating >= desired_rating) {
                rating_above_desired = true;
                break;
            }
        }
        if (rating_above_desired) {
            write_line(db.movies[i].name);
            found = true;
        }
    }
    if (!found) {
        write_line("No movies found with a rating above or equal to " + to_string(desired_rating) + ".");
    }
}

int main() {
    Database db; // Create the database object
    main_menu(db); // Call the main menu function with the database object
    return 0;
}

Honestly never encountered this problem before. Its really annoying me. I’ve been told that it might have something to do with the library itself?

New contributor

shzpes is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật