DVD Collection program

This program is supposed to utilize two classes. One class is to hold the information for a DVD. The other class is supposed to maintain the dvd collection, such as add a dvd, remove dvd, delete dvd, and view the collection. I am having a really hard time figuring out how to get all of my code to work together. I have a large chunk of code typed out, but unfortunately it isn't all working as it should. I am really at a loss as to why my display function is not working.

MainFile.cpp
------------------------------------
#include "DVD.h"
#include "Maintain.h"
#include <iostream>
#include <string>

using namespace std;

// function prototype
int menu();
void display(DVD *);

// global
fstream dataFile;
int i = 0;

int main()
{
// variables
DVD dvd; // instance of DVD
Maintain maintain; // instance of maintain DVD
string title; // to hold movie title
double length; // to hold movie length
int year; // to hold year released
string firstActor; // to hold first main actor
string secondActor; // to hold second main actor
fstream dataFile; // file object
int choice; // to hold user menu choice
DVD collect[5];
int n; // to hold dvd number

// open file
dataFile.open("collection.txt", ios::in | ios::out);

// menu constants
const int SHOW = 1;
const int ADD = 2;
const int REMOVE = 3;
const int UPDATE = 4;
const int EXIT = 5;

// do loop to call for menu
do
{
choice = menu();

// display collection to user
if (choice == 1)
{
display(collect);
}

// allow user to enter info
if (choice == 2)
{
// Ask user to enter DVD info
cout << "Enter movie title: ";
cin.ignore();
getline(cin, title);
cout << "Enter movie length: ";
cin >> length;
cout << "Enter year released: ";
cin >> year;
cout << "Enter one of the main actors: ";
cin.ignore();
getline(cin, firstActor);
cout << "Enter another one of the main actors: ";
cin.ignore();
getline(cin, secondActor);
cout << endl;
cout << "The movie has been added\n";

// Store the DVD
collect[i].setTitle(title);
collect[i].setLength(length);
collect[i].setYear(year);
collect[i].setFirstActor(firstActor);
collect[i].setSecondActor(secondActor);
}

// allow user to remove DVD
if (choice == 3)
{


}

// provide option to update DVD information
if (choice == 4)
{
// display information so the user knows what they are updating
display(collect);

cout << "Enter the number of the dvd you would like to update: ";
cin >> n;

// Ask user to enter DVD info
cout << "Enter movie title: ";
cin.ignore();
getline(cin, title);
cout << "Enter movie length: ";
cin >> length;
cout << "Enter year released: ";
cin >> year;
cout << "Enter one of the main actors: ";
cin.ignore();
getline(cin, firstActor);
cout << "Enter another one of the main actors: ";
cin.ignore();
getline(cin, secondActor);
cout << endl;
cout << "The movie has been added\n";

// Store the DVD
collect[n-1].setTitle(title);
collect[n-1].setLength(length);
collect[n-1].setYear(year);
collect[n-1].setFirstActor(firstActor);
collect[n-1].setSecondActor(secondActor);
cout << endl;
cout << "The DVD information has been updated\n";
}

// provide option for user to exit
if (choice == 5)
{
exit(0);
}

} while (choice >= 1 && choice <= 5);



// end of file
system("pause");
return 0;
}

/*
int menu()
This function will show a menu for the user
to make a selection from
*/
int menu()
{
// variable
int c;

cout << endl;
cout << "\tDVD Collection\n";
cout << "----------------------\n";
cout << endl;
cout << "1. Show Collection" << endl;
cout << "2. Add DVD" << endl;
cout << "3. Remove DVD" << endl;
cout << "4. Update DVD" << endl;
cout << "5. Exit" << endl;
cout << "Enter 1-5 \n";
cin >> c;

return c;
}

/*
void display()
This function will display the DVD collection
the the user
*/
void display(DVD *dvd)
{
for (int a = 0; a < i; a++)
{
cout << "Dvd No. : " << a + 1 << endl;
cout << "Title : " << dvd[a].getTitle() << endl;
cout << "Length: " << dvd[a].getLength() << endl;
cout << "Year: " << dvd[a].getYear() << endl;
cout << "First Main Actor: " << dvd[a].getFirstActor() << endl;
cout << "Second Main Actor: " << dvd[a].getSecondActor() << endl;
cout << endl;
}

}


DVD.h
----------------------
#pragma once
#ifndef DVD_H
#define DVD_H
#include <iostream>
#include <string>
using namespace std;

class DVD
{
private:
string title;
int year;
double length;
string firstMainActor;
string secondMainActor;

public:
void setTitle(string);
void setYear(int);
void setLength(double);
void setFirstActor(string);
void setSecondActor(string);
string getTitle() const;
int getYear() const;
double getLength() const;
string getFirstActor() const;
string getSecondActor() const;
DVD();
DVD(string, double, int, string, string);
};

#endif


DVD.cpp
---------------------------------
#include "DVD.h"
#include <iostream>
#include <vector>
#include <string>
using namespace std;

/* constructor */
DVD::DVD()
{
title = " ";
year = 0;
length = 0;
firstMainActor = " ";
secondMainActor = " ";
}

DVD::DVD(string t, double l, int y, string first, string second)
{
title = t;
length = l;
year = y;
firstMainActor = first;
secondMainActor = second;
}

/* This function will set the title of the movie*/
void DVD::setTitle(string t)
{
title = t;
}

/* This function will set the year of the movie */
void DVD::setYear(int y)
{
year = y;
}

/* This function will set the length of the movie */
void DVD::setLength(double l)
{
length = l;
}

/* This function will set the name of the first main actor
in the movie */
void DVD::setFirstActor(string first)
{
firstMainActor = first;
}

/* This function will set the name of the second main actor
in the movie */
void DVD::setSecondActor(string second)
{
secondMainActor = second;
}

/* This function will return the title of the movie */
string DVD::getTitle() const
{
return title;
}

/* This function will return the year of the movie */
int DVD::getYear() const
{
return year;
}

/* This function will return the length of the movie */
double DVD::getLength() const
{
return length;
}

/* This function will return the first main actor of
the movie */
string DVD::getFirstActor() const
{
return firstMainActor;
}

/* This function will return the second main actor of
the movie */
string DVD::getSecondActor() const
{
return secondMainActor;
}

Last edited on
http://www.cplusplus.com/articles/jEywvCM9/
Please add code tags to your post to make your code easier to read.
Hello Tipper1997,

PLEASE ALWAYS USE CODE TAGS (the <> formatting button), to the right of this box, when posting code.

Along with the proper indenting it makes it easier to read your code and also easier to respond to your post.

http://www.cplusplus.com/articles/jEywvCM9/
http://www.cplusplus.com/articles/z13hAqkS/

Hint: You can edit your post, highlight your code and press the <> formatting button.
You can use the preview button at the bottom to see how it looks.

I found the second link to be the most help.



Have a little problem. Your program does not compile because the header file "Maintain.h" is missing.

Andy
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
#include <iostream>
#include <string>
#include <vector>
#include <iomanip>

// One class is to hold the information for a DVD
class DVD
{
    public:

        DVD() = default ;

        DVD( std::string title, double len, int year,
             std::string first_actor = "", std::string second_actor = "" )
            : title(title), year(year), length(len),
              firstMainActor(first_actor), secondMainActor(second_actor)
        {
            if( year < 1600 ) year = 1600 ;
            if( length < 0 ) length = 0 ;
        }

        const std::string& album_name() const { return title ; }
        // etc.

    private:

        std::string title = "unspecified" ;
        int year = 2019 ;
        double length = 0 ;
        std::string firstMainActor;
        std::string secondMainActor;

    // https://en.cppreference.com/w/cpp/language/operators#Stream_extraction_and_insertion
    friend std::ostream& operator<< ( std::ostream& stm, const DVD& dvd )
    {
        return stm << "dvd{ title: " << std::quoted(dvd.title)
                   << ", year: " << dvd.year
                   << ", duration: " << dvd.length << " minutes }" ;
    }
};

// The other class is supposed to maintain the dvd collection
class dvd_collection
{
    public:

        void add( DVD dvd ) { collection.push_back( std::move(dvd) ) ; }

        void add( std::string title, double len, int year,
                  std::string first_actor = "", std::string second_actor = "" )
        {
            // https://en.cppreference.com/w/cpp/container/vector/emplace_back
            collection.emplace_back( title, len, year, first_actor, second_actor ) ;
        }

        void print() const
        {
            for( const DVD& dvd : collection ) std::cout << dvd << '\n' ;
        }

        // TO DO: erase/remove

    private:
        std::vector<DVD> collection ; // https://cal-linux.com/tutorials/vectors.html
};

int main()
{
    dvd_collection my_dvds ;

    my_dvds.add( "Stalker (Tarkovsky)", 180, 1979 ) ;
    my_dvds.add( "Tokyo Story (Ozu)", 135, 1953 ) ;
    my_dvds.add( "Werckmeister Harmoniak (Tarr)", 160, 2000 ) ;

    my_dvds.print() ;
}

http://coliru.stacked-crooked.com/a/9198acfd2429a2a7

Take it up from there.
Topic archived. No new replies allowed.