Thread: DVD rental application.

  1. #1
    Registered User
    Join Date
    Jan 2012
    Posts
    29

    DVD rental application.

    Hello there!

    I'm in a middle of my DVD rental project. It's my first bigger thing in c++ and I had my c++ classes long time ago, so I have a few issues and difficulties.

    So, I have a class:

    Code:
    class movie{
    private:
        string title;
        string director;
        string scenario;
        int year;
        string genre;
        int lenght;
        string country;
        string leading_role1;
        string leading_role2;
        string leading_role3;
        string leading_role4;
        string leading_role5;
        double price;
        
    public:
        void setMovie();
        void deleteMovie();
        void getMovie();
        int compareMovie(string);
    };
    and I allocate memory for an array of its objects:

    Code:
    movie *ptr_movie;
    ptr_movie=new movie[SIZE];
    And here is my first question: how is that dynamic memory? I have SIZE elements in it. SIZE is a number defined earlier or when program asks for it (so maybe 'cause of this 2nd thing it's dynamic?) so when I reach this number program would crash, wouldn't it?




    Let's assume I have 3 movies in my base, there are available under indexes [0], [1], [2]. And now I want to erase element in the middle. So, 1) how do I remove it? By delete[1] ptr_movie? Intuition says something is wrong, if it's not - what happens with array? 2) When I remove 2nd element, should I shift all elements that stands next to it?

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,633
    You may want to consider using std::vector instead of arrays.
    And here is my first question: how is that dynamic memory? I have SIZE elements in it. SIZE is a number defined earlier or when program asks for it (so maybe 'cause of this 2nd thing it's dynamic?) so when I reach this number program would crash, wouldn't it?
    Since a normal "static" array is created using a compile time constant,
    Code:
    int array[10];
    const int size = 10;
    int array2[size];
    to create an array that doesn't use a compile time constant you need to use dynamic memory (new/delete). In C++ arrays start at zero and end at size -1. This is true regardless of the type array, dynamic or static. If you try to access the array by using and index of size, your program could crash, but not necessarily.
    Let's assume I have 3 movies in my base, there are available under indexes [0], [1], [2]. And now I want to erase element in the middle. So, 1) how do I remove it?
    Once you create an array, either statically or dynamically, this array is of a fixed size. You can't re-size an array. This is really where std::vector can be of assistance, this container class is dynamic. It can grow to accommodate more items, or you can remove an item from the vector. All of this is done by using the proper class members, push_back(), insert(), erase(), pop_back(), etc.

    Jim

  3. #3
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Quote Originally Posted by jimblumberg View Post
    You may want to consider using std::vector instead of arrays.
    I would suggest using lists

  4. #4
    Registered User
    Join Date
    May 2010
    Posts
    4,633
    Why? The OP was referring to dynamic arrays, which is one of the definitions of std::vector. Also with a list you don't have the ability to randomly access the elements. In my opinion you select std::list only when you don't need random access and you need to insert elements into the middle of the container. Also std::vector is in my opinion works similar to the way arrays work and so I normally use this class to replace arrays. I may select one of the other containers if I have special needs.

    Jim

  5. #5
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    And here is my first question: how is that dynamic memory? I have SIZE elements in it. SIZE is a number defined earlier or when program asks for it (so maybe 'cause of this 2nd thing it's dynamic?) so when I reach this number program would crash, wouldn't it?
    The array size can be defined by a value determined at run-time. Additionally, since it is also possible to reassign pointers, you do have the possibility of growing or shrinking the array.

    Let's assume I have 3 movies in my base, there are available under indexes [0], [1], [2]. And now I want to erase element in the middle. So, 1) how do I remove it? By delete[1] ptr_movie? Intuition says something is wrong, if it's not - what happens with array? 2) When I remove 2nd element, should I shift all elements that stands next to it?
    Shift old elements down to the end of the array. You will need some extra space to hold onto elements you want to move while you move the other elements up. Then you can resize, shrinking the array to a smaller size.

    The difficulty in all of this is the reason why there are other data structures, like linked lists. The difficulty in managing memory is part of the reason the STL containers are there to help you.
    Last edited by whiteflags; 08-26-2012 at 09:25 AM.

  6. #6
    Registered User
    Join Date
    Jan 2012
    Posts
    29
    Quote Originally Posted by jimblumberg View Post
    You may want to consider using std::vector instead of arrays.
    Because it's not the first time today I see someone's mentioned these vectors I decided to give it a shot... and I'm confused a litte.

    So I have my class movie, I declare vector:

    Code:
    vector <movie> base;
    How do I access my methods, in this case setMovie()?

    Code:
    void movie::setMovie(){
        cout<<"  What is the title? ";
        getline(cin,title);        
        cout<<"  Who is the director? ";
        getline(cin,director);
        cout<<"  Who wrote scenario? ";
        getline(cin,scenario);    
        cout<<"  When the movie has been released? ";
        cin>>year;
        cin.get(); //"swalows" \n that has been left in buffor
        cout<<"  What is the genre? ";
        getline(cin,genre); 
        cout<<"  How long is the film (in minutes)? ";
        cin>>lenght;
        cin.get(); //"swalows" \n that has been left in buffor
        cout<<"  What's the country of the movie? ";
        getline(cin,country);
        cout<<"  Type names of 3 actors that play main roles: ";
        getline(cin,leading_role1,'\n');
        cout<<"  ";
        getline(cin,leading_role2,'\n');        
        cout<<"  ";
        getline(cin,leading_role3,'\n');
        cout<<"  What is the price per day?: ";
        cin>>price;
        cin.get(); //"swalows" \n that has been left in buffor
    }
    Code:
    base.push_back(???)
    
    //this is where I want to
    //add in the end of a vector
    //an object by calling method setMovie()

  7. #7
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    How do I access my methods, in this case setMovie()?
    Vector is meant to look and act like an array, so you would access class methods of elements in the vector the same way you do in arrays.

  8. #8
    Registered User
    Join Date
    May 2010
    Posts
    4,633
    First the element must exist, so for your class something like:

    Code:
       vector<movie> movies; // Create a vector of movie.
       movies.push_back(movie()); // Add a blank movie to the vector.
       movies[0].setMovie(); // Set the values of movie[0]
    Once you have elements in your vector you access it just like you would an array.

    Jim

  9. #9
    Registered User
    Join Date
    Jan 2012
    Posts
    29
    I use counter which role is to remember how movies are in base, I write an example so You could see what I mean:

    Code:
    int id_movie=0;
    (...)
    movie_base[id_movie].setMovie();
    id_movie++;
    Every time I add a movie I see how many movies are there, I have it in id_movie variable. So if I'd like to use erase() I have to remember to decrease variable by 1 right? Function shifts elements in array all by itself?

  10. #10
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    So if I'd like to use erase() I have to remember to decrease variable by 1 right? Function shifts elements in array all by itself?
    Yes.

  11. #11
    Registered User
    Join Date
    May 2010
    Posts
    4,633
    You don't even need to keep track of how many elements are in your vector, the vector knows it's size. To have the vector show it's size you use the size() member function.


    Jim

  12. #12
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,472
    Just an idea on your class definition - list members in order public first , protected, private - Its just a convention, idea being other progammers only need read applicable parts of the class - and public being the most broadly accessed applies to more people so is shown first and no need to scroll further

    Also without wanting to divert you too much i would consider the class being MovieLibrary and contain a vector of structs that describe the movies contained - then you declare an instance of MovieLibrary and start populating the vector.

    This means you can declare MovieLibrary drama etc - Further to this is the idea of superclass MovieStoreStock
    Last edited by rogster001; 08-27-2012 at 03:54 AM.
    Thought for the day:
    "Are you sure your sanity chip is fully screwed in sir?" (Kryten)
    FLTK: "The most fun you can have with your clothes on."

    Stroustrup:
    "If I had thought of it and had some marketing sense every computer and just about any gadget would have had a little 'C++ Inside' sticker on it'"

  13. #13
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    You could also do

    vector<movie> movies; // Create a vector of movie.
    movies.push_back(movie()); // Add a blank movie to the vector.
    movies.back()->setMovie(); // Set the values of the last movie

    Since back() always returns an iterator to the last element.
    Btw, you are misusing objects. An object is supposed to be an instance of a movie. Therefore, the getMovie member doesn't make sense. You should probably have some map or another associative array that contains your movies from which you can pick the one you're interested in.
    Furthermore, setMovie should be the constructor and should take in relevant parameters (create a free function CreateMovie that inputs necessary parameters and returns a Movie object). This makes your code more flexible. You might decide to read into movie objects from a file sometime in the future.
    Finally, deleteMovie doesn't make sense. Your instance IS the movie. If you don't want it, then get rid of the instance. Simple.
    Last edited by Elysia; 08-27-2012 at 11:17 AM.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  14. #14
    Registered User
    Join Date
    Jan 2012
    Posts
    29
    Thanks for help! There are also few good ideas that I could use BUT:
    first of all I'd like it to be fully-working program. For now I have two classes: movie and customer. I can add and delete objects, You can get more information about movie or customer. To get it done I need at least 2 things: an idea of renting process and writing (and reading) results of my work into the txt file.

    1. My idea: add to a class customer, let's say 3 strings named 'title1, title2, title3'. And when when You rent a movie, title goes there. But sounds silly to me.

    2. C++ is supposed to be simpler than C but I cannot figure it. In C I opened a file with fopen, read it few lines later, close it with fclose, and write it all down with fwrite just before fclose is executed. I just can't figure it out in C++.

  15. #15
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by blunt View Post
    2. C++ is supposed to be simpler than C but I cannot figure it. In C I opened a file with fopen, read it few lines later, close it with fclose, and write it all down with fwrite just before fclose is executed. I just can't figure it out in C++.
    C++ will also flush the buffer before you close a file. If you can reproduce a situation where this does not seem to be the case, then post the smallest possible compilable example that demonstrates the problem.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Car Rental Program
    By iamtazb in forum C Programming
    Replies: 4
    Last Post: 06-18-2012, 05:05 PM
  2. Replies: 2
    Last Post: 05-18-2012, 04:20 AM
  3. Help - Car Rite Rental (extra credit)
    By mmourot in forum C++ Programming
    Replies: 5
    Last Post: 10-13-2011, 07:00 AM
  4. Replies: 1
    Last Post: 07-03-2010, 01:18 PM
  5. Convirt my C program to a GUI application/Web Application
    By kapil1089thekin in forum C Programming
    Replies: 6
    Last Post: 07-21-2008, 01:43 AM