How to create an array of objects of another class?

  • (4 Pages)
  • +
  • 1
  • 2
  • 3
  • 4

47 Replies - 1346 Views - Last Post: 08 July 2013 - 01:34 PM Rate Topic: -----

#16 jordannn15  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 215
  • Joined: 06-July 12

Re: How to create an array of objects of another class?

Posted 06 July 2013 - 07:32 PM

Well no I shouldn't be just swapping the title should I? In my menu function I have the option to sort the movies by title so when I sort them I would have to sort the movies not just the titles correct?
Was This Post Helpful? 0
  • +
  • -

#17 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1432
  • View blog
  • Posts: 4,968
  • Joined: 19-February 09

Re: How to create an array of objects of another class?

Posted 06 July 2013 - 07:58 PM

View Post#define, on 07 July 2013 - 03:15 AM, said:

When you are sorting you are not swapping the movie just trying to swap the title :


Hi, yes it could be clearer.

When you're sorting you are trying to swap the title. You should be swapping the whole movie/dvd:

You have:

021	     tmp = movies[i].getTitle();
022	     movies[i].getTitle() = movies[j + 1].getTitle();
023	     movies[j + 1].getTitle() = tmp;
024	                               }



'

This post has been edited by #define: 06 July 2013 - 07:59 PM

Was This Post Helpful? 0
  • +
  • -

#18 jimblumberg  Icon User is offline

  • member icon


Reputation: 4290
  • View blog
  • Posts: 13,454
  • Joined: 25-December 09

Re: How to create an array of objects of another class?

Posted 06 July 2013 - 08:16 PM

Please post a sample of your input file.


Jim
Was This Post Helpful? 0
  • +
  • -

#19 jordannn15  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 215
  • Joined: 06-July 12

Re: How to create an array of objects of another class?

Posted 06 July 2013 - 08:26 PM

View Postjimblumberg, on 06 July 2013 - 08:16 PM, said:

Please post a sample of your input file.


Jim


I don't have an input file the point of the load_from_file and save_to_file is so that after the first time the program runs your whole dvd library is still there from last time.
Was This Post Helpful? 0
  • +
  • -

#20 jimblumberg  Icon User is offline

  • member icon


Reputation: 4290
  • View blog
  • Posts: 13,454
  • Joined: 25-December 09

Re: How to create an array of objects of another class?

Posted 06 July 2013 - 08:31 PM

Okay then the first thing you should be working on is your entry function. At present you don't have anyway of entering a dvd into your array. How do you plan to allow the user to enter a dvd into your library?

Jim
Was This Post Helpful? 0
  • +
  • -

#21 jordannn15  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 215
  • Joined: 06-July 12

Re: How to create an array of objects of another class?

Posted 06 July 2013 - 08:42 PM

Okay, so changing it now I only have one error:

C:\All Coding Stuff\C++ Coding\Projects\Project1\dvdLib.cpp In member function `void dvdLib::remove(dvd&)':

84 C:\All Coding Stuff\C++ Coding\Projects\Project1\dvdLib.cpp no match for 'operator[]' in '((dvdLib*)this)->dvdLib::movies[item]'

//dvdLib.h

#ifndef DVDLIB_H
#define DVDLIB_H
#include <iostream>
#include <cstdlib>
#include "dvd.h"
using namespace std;

class dvdLib {
      public:
             static const size_t CAPACITY = 200;

             void seeAll(ostream& out);
             void sortA();
             void sortY();
             void sortR();
             
             void search(string target);
             void insert(dvd& item);
             void remove(dvd& item);
             
             void load_from_file(istream& ins);
             char menu(char& choice);
             void save_to_file(ostream& outs);
        
      private:
             dvd movies[CAPACITY];
             size_t used;
};

#endif



//dvdLib.cc File

#include <iostream>
#include <cstdlib>
#include <fstream>
#include "dvd.h"
#include "dvdLib.h"
using namespace std;

void dvdLib::seeAll(ostream& out){
     for(unsigned int i = 0; i < used; i++)
        cout << movies[i] << endl;
}

void dvdLib::sortA(){
     dvd tmp;
     dvdLib temp;
     
     for(unsigned int i = 0; i < used; i++){
                  for(unsigned int j = 0; j < used - 1; j++){
                               if(movies[i].getTitle() > movies[i + 1].getTitle()){
                                                       tmp = movies[i];
                                                       movies[i] = movies[i + 1];
                                                       movies[i + 1] = tmp;
                               }
                  }
     }
     
     temp.seeAll(cout);
}
 
void dvdLib::sortY(){
     dvd tmp;
     dvdLib temp;
     
     for(unsigned int i = 0; i < used; i++){
                  for(unsigned int j = 0; j < used - 1; j++){
                               if(movies[i].getYear() > movies[i + 1].getYear()){
                                                       tmp = movies[i];
                                                       movies[i] = movies[i + 1];
                                                       movies[i + 1] = tmp;
                               }
                  }
     }
     
     temp.seeAll(cout);
}

void dvdLib::sortR(){
     dvd tmp;
     dvdLib temp;
     
     for(unsigned int i = 0; i < used; i++){
                  for(unsigned int j = 0; j < used - 1; j++){
                               if(movies[i].getRating() > movies[i + 1].getRating()){
                                                       tmp = movies[i];
                                                       movies[i] = movies[i + 1];
                                                       movies[i + 1] = tmp;
                               }
                  }
     }
     
     temp.seeAll(cout);
}

void dvdLib::search(string target){
     size_t i = 0;
     
     while(i < used && movies[i].getTitle() != target) ++i;
     if(i == used)
        cout << "Not found.\n";
     else
        cout << movies[i] << endl;
}
                             
void dvdLib::insert(dvd& item){
     if(used < CAPACITY){
             movies[used] = item;
             used++;
     }
     else
        cout << "Error. List is full.\n";
}

void dvdLib::remove(dvd& item){
     movies[item] = movies[used - 1];
     used--;
}

void dvdLib::load_from_file(istream& ins){
     dvd tmp;
     ifstream ifs("movies.dat");
     
     while(ins >> tmp)
        insert(tmp);
}

char menu(char& choice){
     dvdLib obj;
     dvd item;
     string target;
     
     while(choice != '8'){
                  cout << "1. Insert a movie.\n";
                  cout << "2. Remove a movie.\n";
                  cout << "3. Display all movies.\n";
                  cout << "4. Search for a movie by title.\n";
                  cout << "5. Display all movies alphabetically.\n";
                  cout << "6. Display all movies by year.\n";
                  cout << "7. Display all movies by rating.\n";
                  cout << "8. Quit.\n\n";
                  cout << "Enter your choice ->   ";
                  cin >> choice;
                  
     if(choice == '1')
       obj.insert(item);
     else if(choice == '2')
       obj.remove(item);
     else if(choice == '3')
       obj.seeAll(cout);
     else if(choice == '4')
       obj.search(target);
     else if(choice == '5')
       obj.sortA();
     else if(choice == '6')
       obj.sortY();
     else if(choice == '7')
       obj.sortR();
     else if(choice == '8')
       cout << "Goodbye.\n";
     else
       cout << "Invalid choice. Pick a choice (1 - 8)\n";
     }
}

void dvdLib::save_to_file(ostream& outs){
     for(unsigned int i = 0; i < used; i++)
        outs << movies[i] << endl;
}



//main.cc File

#include <iostream>
#include <fstream>
#include <cstdlib>
#include "dvd.h"
#include "dvdLib.h"
using namespace std;

int main(){
    dvdLib movies;
    char choice;
    
    ifstream ifs("movies.dat");
    if(!ifs.fail())
      movies.load_from_file(ifs);
    ifs.close();
    
    movies.menu(choice);
    
    ofstream ofs("movies.dat");
    if(!ofs.fail())
      movies.save_to_file(ofs);
    ofs.close();

    return 0;
}



View Postjimblumberg, on 06 July 2013 - 08:31 PM, said:

Okay then the first thing you should be working on is your entry function. At present you don't have anyway of entering a dvd into your array. How do you plan to allow the user to enter a dvd into your library?

Jim


In my dvd.h and dvd.cc file I originally posted I had an input and output function along with overloading my << and >> operators and just called the input and output functions. Is that what you mean?
Was This Post Helpful? 0
  • +
  • -

#22 jordannn15  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 215
  • Joined: 06-July 12

Re: How to create an array of objects of another class?

Posted 06 July 2013 - 08:56 PM

@jim Oh I see what you mean I haven't called my input() function anywhere in my dvdLib.cc file yet. I fixed that.
Was This Post Helpful? 0
  • +
  • -

#23 jimblumberg  Icon User is offline

  • member icon


Reputation: 4290
  • View blog
  • Posts: 13,454
  • Joined: 25-December 09

Re: How to create an array of objects of another class?

Posted 06 July 2013 - 09:02 PM

For your errors, right now I would comment out everything in the remove function, you'll need to work on it later.



Jim
Was This Post Helpful? 0
  • +
  • -

#24 jordannn15  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 215
  • Joined: 06-July 12

Re: How to create an array of objects of another class?

Posted 06 July 2013 - 09:33 PM

Okay, so right now the program will run. I can add a movie but when I try to display it, it will display 0 0 for some reason. Also, I can remove a movie but not one that I choose just removes the very last one I inserted. I didn't test the sorts yet and obviously the quit option works fine. Oh and the second time I run the program it prompts me with what is in the input function, and it's just an infinite loop that keeps asking me info about the movie.

//dvdLib.h File

#ifndef DVDLIB_H
#define DVDLIB_H
#include <iostream>
#include <cstdlib>
#include "dvd.h"
using namespace std;

class dvdLib {
      public:
             static const size_t CAPACITY = 200;

             void seeAll(ostream& out);
             void sortA();
             void sortY();
             void sortR();
             
             void search(string target);
             void insert(dvd& item);
             void remove();
             
             void load_from_file(istream& ins);
             void menu(char& choice);
             void save_to_file(ostream& outs);
        
      private:
             dvd movies[CAPACITY];
             size_t used;
};

#endif



//dvdLib.cc File

#include <iostream>
#include <cstdlib>
#include <fstream>
#include "dvd.h"
#include "dvdLib.h"
using namespace std;

void dvdLib::seeAll(ostream& out){
     for(int i = 0; i < used; i++)
        out << movies[i] << endl;
}

void dvdLib::sortA(){
     dvd tmp;
     dvdLib temp;
     
     for(unsigned int i = 0; i < used; i++){
                  for(unsigned int j = 0; j < used - 1; j++){
                               if(movies[i].getTitle() > movies[j + 1].getTitle()){
                                                       tmp = movies[i];
                                                       movies[i] = movies[j + 1];
                                                       movies[j + 1] = tmp;
                               }
                  }
     }
     
     temp.seeAll(cout);
}
 
void dvdLib::sortY(){
     dvd tmp;
     dvdLib temp;
     
     for(unsigned int i = 0; i < used; i++){
                  for(unsigned int j = 0; j < used - 1; j++){
                               if(movies[i].getYear() > movies[j + 1].getYear()){
                                                       tmp = movies[i];
                                                       movies[i] = movies[j + 1];
                                                       movies[j + 1] = tmp;
                               }
                  }
     }
     
     temp.seeAll(cout);
}

void dvdLib::sortR(){
     dvd tmp;
     dvdLib temp;
     
     for(unsigned int i = 0; i < used; i++){
                  for(unsigned int j = 0; j < used - 1; j++){
                               if(movies[i].getRating() > movies[j + 1].getRating()){
                                                       tmp = movies[i];
                                                       movies[i] = movies[j + 1];
                                                       movies[j + 1] = tmp;
                               }
                  }
     }
     
     temp.seeAll(cout);
}

void dvdLib::search(string target){
     size_t i = 0;
     
     cout << "Enter a movie title you want to find:  ";
     getline(cin, target);
     
     while(i < used && movies[i].getTitle() != target) ++i;
     if(i == used)
        cout << "Not found.\n";
     else
        cout << movies[i] << endl;
}
                             
void dvdLib::insert(dvd& item){
     dvd obj;
     obj.input();
     
     if(used < CAPACITY){
             movies[used] = item;
             used++;
     }
     else
        cout << "Error. List is full.\n";
}

void dvdLib::remove(){
     movies[used] = movies[used - 1];
     used--;
}

void dvdLib::load_from_file(istream& ins){
     dvd tmp;
     ifstream ifs("movies.dat");
     
     while(ins >> tmp)
        insert(tmp);
}

void dvdLib::menu(char& choice){
     dvdLib obj;
     dvd item;
     string target;
     
     while(choice != '8'){
                  cout << "1. Insert a movie.\n";
                  cout << "2. Remove a movie.\n";
                  cout << "3. Display all movies.\n";
                  cout << "4. Search for a movie by title.\n";
                  cout << "5. Display all movies alphabetically.\n";
                  cout << "6. Display all movies by year.\n";
                  cout << "7. Display all movies by rating.\n";
                  cout << "8. Quit.\n\n";
                  cout << "Enter your choice ->   ";
                  cin >> choice;
                  
     if(choice == '1')
       obj.insert(item);
     else if(choice == '2')
       obj.remove();
     else if(choice == '3')
       obj.seeAll(cout);
     else if(choice == '4')
       obj.search(target);
     else if(choice == '5')
       obj.sortA();
     else if(choice == '6')
       obj.sortY();
     else if(choice == '7')
       obj.sortR();
     else if(choice == '8')
       cout << "Goodbye.\n";
     else
       cout << "Invalid choice. Pick a choice (1 - 8)\n";
     }
}

void dvdLib::save_to_file(ostream& outs){
     for(unsigned int i = 0; i < used; i++)
        outs << movies[i] << endl;
}



//main.cc File

#include <iostream>
#include <fstream>
#include <cstdlib>
#include "dvd.h"
#include "dvdLib.h"
using namespace std;

int main(){
    dvdLib movie;
    char choice;
    
    ifstream ifs("movies.dat");
    if(!ifs.fail())
      movie.load_from_file(ifs);
    ifs.close();
    
    movie.menu(choice);
    
    ofstream ofs("movies.dat");
    if(!ofs.fail())
      movie.save_to_file(ofs);
    ofs.close();

    return 0;
}


Was This Post Helpful? 0
  • +
  • -

#25 jordannn15  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 215
  • Joined: 06-July 12

Re: How to create an array of objects of another class?

Posted 06 July 2013 - 10:40 PM

Any other advice please post it I'm getting off for the night. Will check back tomorrow. Thanks for the help so far.
Was This Post Helpful? 0
  • +
  • -

#26 jimblumberg  Icon User is offline

  • member icon


Reputation: 4290
  • View blog
  • Posts: 13,454
  • Joined: 25-December 09

Re: How to create an array of objects of another class?

Posted 07 July 2013 - 05:06 AM

Look at this snippet:
void dvdLib::insert(dvd& item) {
   dvd obj;
   obj.input();

   if(used < CAPACITY) {
      movies[used] = item;
      used++;
   }
   else
      cout << "Error. List is full.\n";
}


I have a couple of questions.

First why are you passing a a dvd reference into this function?

Why are you creating a temporary dvd in this function?

I really don't see why you need to pass a dvd reference into this function. Where do you ever use this value in the calling function?

Most of the functions in your "menu" don't need a parameter, they're operating on the current instance of your dvdLib class. They store and manipulate the data in this class so they don't need any parameters.

The same with your menu() function. Why are you passing the reference to the character into this function? Where are you using this value in main()?

You also need to think about creating a constructor for your dvdLib class so you can properly initialize your variables. Right now the first time you run this program if you try to display the records you are invoking undefined behavior because used is not initialized.

One other thing. In your class definition you declared unit to be a size_t, which is good. But elsewhere, to quite your compiler you use unsigned int when comparing to this size_t. You should be using size_t throughout. A size_t can be any unsigned integral type, not necessarily an unsigned int. It could be an unsigned long long or some other unsigned type.

Jim

This post has been edited by jimblumberg: 07 July 2013 - 05:07 AM

Was This Post Helpful? 0
  • +
  • -

#27 jordannn15  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 215
  • Joined: 06-July 12

Re: How to create an array of objects of another class?

Posted 07 July 2013 - 12:56 PM

View Postjimblumberg, on 07 July 2013 - 05:06 AM, said:

Look at this snippet:
void dvdLib::insert(dvd& item) {
   dvd obj;
   obj.input();

   if(used < CAPACITY) {
      movies[used] = item;
      used++;
   }
   else
      cout << "Error. List is full.\n";
}


I have a couple of questions.

First why are you passing a a dvd reference into this function?

Answer: I'm passing it in because that's what I am adding correct a dvd object into the array of dvd's?

Quote

Why are you creating a temporary dvd in this function?

Answer: I needed an object of dvd class to call the input function or atleast I thought I did.

Quote

I really don't see why you need to pass a dvd reference into this function. Where do you ever use this value in the calling function?

Answer: In the line where I say movies[used] = item; Should I be using something else for this?

Quote

Most of the functions in your "menu" don't need a parameter, they're operating on the current instance of your dvdLib class. They store and manipulate the data in this class so they don't need any parameters.


The same with your menu() function. Why are you passing the reference to the character into this function? Where are you using this value in main()?

Answer: Yeah I changed it from having a parameter to just have a local variable and returning it.

Quote

You also need to think about creating a constructor for your dvdLib class so you can properly initialize your variables. Right now the first time you run this program if you try to display the records you are invoking undefined behavior because used is not initialized.

Answer: Thank you I have made one and now when I choose to display all movies it will display the actual info instead of just 0 0.

Quote

One other thing. In your class definition you declared unit to be a size_t, which is good. But elsewhere, to quite your compiler you use unsigned int when comparing to this size_t. You should be using size_t throughout. A size_t can be any unsigned integral type, not necessarily an unsigned int. It could be an unsigned long long or some other unsigned type.

Anwer: Yeah I really didn't know what unsigned int and that stuff meant I have just seen it and used it because it kept giving me an error about comparing signed and unsigned variables but I changed them to size_t now.

This post has been edited by jimblumberg: 07 July 2013 - 03:06 PM
Reason for edit:: Fixed quotes

Was This Post Helpful? 0
  • +
  • -

#28 jimblumberg  Icon User is offline

  • member icon


Reputation: 4290
  • View blog
  • Posts: 13,454
  • Joined: 25-December 09

Re: How to create an array of objects of another class?

Posted 07 July 2013 - 03:13 PM

Quote

Answer: I'm passing it in because that's what I am adding correct a dvd object into the array of dvd's?

Yes you add this object to your array, but you have never put any information into this object. Wouldn't it be better to use the object you added the information into?

Also does your calling function ever actualy use this object?

Answer: NO! So there is no reason to pass anything into this function.

Quote

Answer: I needed an object of dvd class to call the input function or atleast I thought I did.

That's right, but since you have this instance you don't need the parameter.

Quote

Answer: Yeah I changed it from having a parameter to just have a local variable and returning it.

But why are you even returning anything? Does the calling function actually use/need something returned?

Quote

Answer: Thank you I have made one and now when I choose to display all movies it will display the actual info instead of just 0 0.

Good, does that answer all your questions?

If not post your current code and ask specific questions.


Jim
Was This Post Helpful? 0
  • +
  • -

#29 jordannn15  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 215
  • Joined: 06-July 12

Re: How to create an array of objects of another class?

Posted 07 July 2013 - 04:31 PM

Okay, I kind of re-wrote it after watching my professor's lecture again and wrote most of the functions like he did so that will be why I am passing parameters that you say I could do without. Other than that I am only getting one error that I need help fixing.

The error is: dvdLib.cc:36 error C2677: binary '[' : no global operator found which takes type 'std::string' (or there is no acceptable conversion)

//dvdLib.h File

#ifndef DVDLIB_H
#define DVDLIB_H
#include <iostream>
#include <string>
#include <cstdlib>
#include "dvd.h"
using namespace std;

class dvdLib {
public:
	static const size_t CAPACITY = 200;

	dvdLib(size_t u = 0);

	void SeeAll(ostream& outs);
	void insert(dvd& item);
	void remove(string name);
	dvd search(string target);

	void sortT();
	void sortY();
	void sortR();

	void LoadFromFile(istream& ins);
	void menu(int& choice);
	void SaveToFile(ostream& outs);

private:
	dvd movies[CAPACITY];
	size_t used;
};

#endif



//dvdLib.cc File

#include <iostream>
#include <string>
#include <fstream>
#include <cstdlib>
#include "dvd.h"
#include "dvdLib.h"
using namespace std;

dvdLib::dvdLib(size_t u){
	used = u;
}

void dvdLib::SeeAll(ostream& outs){
	for(size_t i = 0; i < used; i++)
		outs << movies[i] << endl;
}

void dvdLib::insert(dvd& item){
	item.input(cin);
	if(used < CAPACITY){
		movies[used] = item;
		used++;
	}
	else
		cout << "Error. List is full.\n";
}

void dvdLib::remove(string name){
	char junk;

	cout << "What movie do you want to remove?\n\n";
	cout << "Enter movie title ->   ";
	cin.get(junk);
	getline(cin, name);

	movies[name] = movies[used - 1];
	used--;
}

dvd dvdLib::search(string target){
	size_t i = 0;
	char junk;

	cout << "What movie do you want to find?\n\n";
	cout << "Enter movie title ->   ";
	cin.get(junk);
	getline(cin, target);

	while(i < used && movies[i].getTitle() != target) ++i;{
		if(i == used)
			cout << "Not found.\n";
		else
			cout << movies[i] << endl;
	}
}

void dvdLib::sortT(){
	dvd tmp;

	for(size_t i = 0; i < used; i++){
		for(size_t j = 0; j < used - 1; j++){
			if(movies[i].getTitle() > movies[j + 1].getTitle()){
				tmp = movies[i];
				movies[i] = movies[j + 1];
				movies[j + 1] = tmp;
			}
		}
	}
}

void dvdLib::sortY(){
	dvd tmp;

	for(size_t i = 0; i < used; i++){
		for(size_t j = 0; j < used -1; j++){
			if(movies[i].getYear() > movies[j + 1].getYear()){
				tmp = movies[i];
				movies[i] = movies[j + 1];
				movies[j + 1] = tmp;
			}
		}
	}
}

void dvdLib::sortR(){
	dvd tmp;

	for(size_t i = 0; i < used; i++){
		for(size_t j = 0; j < used; j++){
			if(movies[i].getRating() > movies[j + 1].getRating()){
				tmp = movies[i];
				movies[i] = movies[j + 1];
				movies[j + 1] = tmp;
			}
		}
	}
}

void dvdLib::LoadFromFile(istream& ins){
	dvd tmp;

	while(ins >> tmp)
		insert(tmp);
}

void menu(int& choice){
	dvdLib obj;
	dvd item;
	string name;

	while(choice != 8){
		cout << "1. Insert a movie.\n";
		cout << "2. Remove a movie.\n";
		cout << "3. Display movie(s).\n";
		cout << "4. Search for a movie.\n";
		cout << "5. Display all movies by title.\n";
		cout << "6. Display all movies by year.\n";
		cout << "7. Display all movies by rating.\n\n";
		cout << "Enter your choice ->   ";
		cin >> choice;

	if(choice == 1)
		obj.insert(item);
	else if(choice == 2)
		obj.remove(name);
	else if(choice == 3)
		obj.SeeAll(cout);
	else if(choice == 4)
		obj.search(name);
	else if(choice == 5){
		obj.sortT();
		obj.SeeAll(cout);
	}
	else if(choice == 6){
		obj.sortY();
		obj.SeeAll(cout);
	}
	else if(choice == 7){
		obj.sortR();
		obj.SeeAll(cout);
	}
	else if(choice == 8)
		cout << "Goodbye.\n";
	}
}

void dvdLib::SaveToFile(ostream& outs){
	for(size_t i = 0; i < used; i++)
		outs << movies[i] << endl;
}



//main.cc File

#include <iostream>
#include <fstream>
#include <cstdlib>
#include "dvd.h"
#include "dvdLib.h"
using namespace std;

int main(){
    dvdLib movie;
	int choice;
    
    ifstream ifs("movies.dat");
    if(!ifs.fail())
      movie.LoadFromFile(ifs);
    ifs.close();
    
    movie.menu(choice);
    
    ofstream ofs("movies.dat");
    if(!ofs.fail())
      movie.SaveToFile(ofs);
    ofs.close();

    return 0;
}


This post has been edited by jordannn15: 07 July 2013 - 04:32 PM

Was This Post Helpful? 0
  • +
  • -

#30 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3663
  • View blog
  • Posts: 11,482
  • Joined: 05-May 12

Re: How to create an array of objects of another class?

Posted 07 July 2013 - 08:02 PM

That's because normal C style arrays cannot be indexed by a string. You can only use some kind of integral type to index such an array in your code here:
void dvdLib::remove(string name){
	char junk;

	cout << "What movie do you want to remove?\n\n";
	cout << "Enter movie title ->   ";
	cin.get(junk);
	getline(cin, name);

	movies[name] = movies[used - 1];
	used--;
}



What you'll need to do is find the index of the movie with that name, and the move all the movies after it down to overwrite that movie.

Depending on how you move the movies, you may need to force the call on the destructor to avoid a memory leak.

This post has been edited by Skydiver: 07 July 2013 - 08:06 PM

Was This Post Helpful? 0
  • +
  • -

  • (4 Pages)
  • +
  • 1
  • 2
  • 3
  • 4