Thursday, 30 January 2025

Code Solutions

#include <iostream>
#include <vector>

using namespace std;

struct movie {
    string title;
    int year;
    string actor;
    double rating;
};

movie newMovie(){
    movie m;
    cin.ignore(); // clear the line first
    cout << "Please enter the title: ";
    getline(cin,m.title);
    cout << "Please enter an actor(Last, First): ";
    getline(cin, m.actor);
    cout << "Please enter the year: ";
    cin >> m.year;
    cout << "Please enter the rating (1-10): ";
    cin >> m.rating;
    cin.ignore();

    return m;
}

void listMovies(vector<movie> &movies){
    for (int i = 0; i < movies.size(); i++){
        cout << "Title: " << movies[i].title << endl;
        cout << "Year: " << movies[i].year << endl;
        cout << "Actor: " << movies[i].actor << endl;
        cout << "Rating: " << movies[i].rating << endl;
        cout << endl;
    }
}

int main()
{
    vector<movie> movies;
    while (true){
        cout << "Would you like to (a)dd a movie, (l)ist movies, or (e)xit? ";
        char choice;
        cin >> choice;
        
        if (choice == 'a'){
            movies.push_back(newMovie());
        }
        else if (choice == 'l'){
            listMovies(movies);
        }
        else break;
    }
    
    return 0;
}

2 comments:

  1. This comment has been removed by the author.

    ReplyDelete
  2. Thanks for sharing these code solutions! They're well-organized and easy to follow—great for learners who need quick, reliable references. It's always helpful to see clean and simple examples like these. Keep up the great work!

    Epicforce Tech

    ReplyDelete