How to create an array of objects of another class?

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

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

#31 jordannn15  Icon User is offline

  • D.I.C Head

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

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

Posted 07 July 2013 - 08:47 PM

Yeah nevermind lol sorry I don't have to remove a certain movie just the very last movie in the array. And plus I haven't learned destructors yet. Right now I'm working on getting my sorts to work properly would you mind helping me?

Heres the code:

//dvdLib.h

#ifndef DVDLIB_H
#define DVDLIB_H
#include <iostream>
#include <string>
#include <cstdlib>
#include "dvd.h"

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

	dvdLib(size_t u = 0);

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

	void SeeAllY(std::ostream& outs);
	void SeeAllG(std::ostream& outs);
	void SeeAllR(std::ostream& outs);

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

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

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

#endif



//dvdLib.cc

#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::SeeAllY(ostream& outs){
	int year;

	cout << "What year do you want?\n\n";
	cout << "Enter movie year ->   ";
	cin >> year;

	for(size_t i = 0; i < used; i++){
		if(movies[i].getYear() == year)
			outs << movies[i] << endl;
	}
}

void dvdLib::SeeAllG(ostream& outs){
	string genre;

	cout << "What genre do you want?\n\n";
	cout << "Enter movie genre ->   ";
	cin >> genre;

	for(size_t i = 0; i < used; i++){
		if(movies[i].getGenre() == genre)
			outs << movies[i] << endl;
	}
}

void dvdLib::SeeAllR(ostream& outs){
	string rating;

	cout << "What ratings do you want?\n\n";
	cout << "Enter movie rating ->   ";
	cin >> rating;

	for(size_t i = 0; i < used; i++){
		if(movies[i].getRating() == rating)
			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){
	movies[used] = movies[used - 1];
	used--;
}

void 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 - 1; 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 dvdLib::menu(int& choice){
	dvdLib obj;
	dvd item;
	string name;

	while(choice != 11){
		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. Search for movies of a certain year.\n";
		cout << "6. Search for movies of a certain genre.\n";
		cout << "7. Search for movies of a certain rating.\n";
		cout << "8. Display all movies by title.\n";
		cout << "9. Display all movies by year.\n";
		cout << "10. Display all movies by rating.\n";
		cout << "11. Quit.\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.SeeAllY(cout);
	else if(choice == 6)
		obj.SeeAllG(cout);
	else if(choice == 7)
		obj.SeeAllR(cout);
	else if(choice == 8){
		obj.sortT();
		obj.SeeAll(cout);
	}
	else if(choice == 9){
		obj.sortY();
		obj.SeeAll(cout);
	}
	else if(choice == 10){
		obj.sortR();
		obj.SeeAll(cout);
	}
	else if(choice == 11)
		cout << "Goodbye.\n";
	else
		cout << "Invalid choice, please pick 1 - 9.\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"

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

	return 0;
}


Was This Post Helpful? 0
  • +
  • -

#32 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3572
  • View blog
  • Posts: 11,107
  • Joined: 05-May 12

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

Posted 07 July 2013 - 09:01 PM

Actually you still have problems with this code:
void dvdLib::remove(string name){
	movies[used] = movies[used - 1];
	used--;
}



What are you trying to do with line #2?

If used == CAPACITY, then you are going to overrun the buffer (and potentially cause a crash).

Also, why do you have a name argument that you are not using?

With regard to your sorting, why are you using j + 1? You must have a reason why other than "just because".
Was This Post Helpful? 0
  • +
  • -

#33 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3572
  • View blog
  • Posts: 11,107
  • Joined: 05-May 12

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

Posted 07 July 2013 - 09:09 PM

Also with regards to sorting, why are you trying to sort by various fields? Based on the requirements you listed your original post, the only sorting that is truly needed is sorting by movie:

View Postjordannn15, on 06 July 2013 - 06:57 PM, said:

The menu should consist of letting the user insert a movie, remove a movie, see all the movies that are currently there, search for a movie by title, see all the movies with a certain rating, see all the movies with a certain year, and see all the movies sorted alphabetically.

Was This Post Helpful? 0
  • +
  • -

#34 jordannn15  Icon User is offline

  • D.I.C Head

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

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

Posted 07 July 2013 - 09:14 PM

View PostSkydiver, on 07 July 2013 - 09:01 PM, said:

Actually you still have problems with this code:
void dvdLib::remove(string name){
	movies[used] = movies[used - 1];
	used--;
}



What are you trying to do with line #2?

Answer: With line #2 my professor told us to do it this way. So basically copy the very last one we just used i.e. used - 1 to the next available index and then do used-- to get rid of it.

If used == CAPACITY, then you are going to overrun the buffer (and potentially cause a crash).

Also, why do you have a name argument that you are not using?

Answer: Sorry forgot to take it out, your right it is not needed anymore.

With regard to your sorting, why are you using j + 1? You must have a reason why other than "just because".

Answer: That was a typo it should be i + 1, correct?

Was This Post Helpful? 0
  • +
  • -

#35 jordannn15  Icon User is offline

  • D.I.C Head

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

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

Posted 07 July 2013 - 09:25 PM

If I leave the sorts as they are exactly and just change it to i + 1 it always gives me two junk values for year and runTime of the first movie I entered. It fixes it if I change it to i + 1 and change the > to <. Why is this? What do I need to fix?
Was This Post Helpful? 0
  • +
  • -

#36 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3572
  • View blog
  • Posts: 11,107
  • Joined: 05-May 12

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

Posted 07 July 2013 - 09:29 PM

View Postjordannn15, on 08 July 2013 - 12:14 AM, said:

Answer: With line #2 my professor told us to do it this way. So basically copy the very last one we just used i.e. used - 1 to the next available index and then do used-- to get rid of it.


Check your class notes. I think you are missing something.

Let's pretend you start off with no movies and you added "Kickass". So you'll end up with:
movie[0] = { "Kickass" }


and used == 1.
Now if we called your remove(), you'll end up with:
movie[0] = { "Kickass" }
movie[1] = { "Kickass" }


and used == 0.

I doubt that you intended to make a copy of the item you are removing just so that you could remove it.

Anyway, you didn't answer my question about used == CAPACITY. Let's pretend CAPACITY was 3, and we added 3 movies. We'll have:
movie[0] = { "A New Hope" }
movie[1] = { "The Empire Strikes Back" }
movie[2] = { "Return of the Jedi" }


and used == 3.

If we called remove(), what would happen would be:
movie[0] = { "A New Hope" }
movie[1] = { "The Empire Strikes Back" }
movie[2] = { "Return of the Jedi" }
movie[3] = { "Return of the Jedi" }


and used == 2. Assuming that things didn't crash, you just wrote into movie[3] which is the 4th element, but you've only allocated space for 3 elements.

This post has been edited by Skydiver: 07 July 2013 - 09:37 PM

Was This Post Helpful? 0
  • +
  • -

#37 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3572
  • View blog
  • Posts: 11,107
  • Joined: 05-May 12

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

Posted 07 July 2013 - 09:36 PM

View Postjordannn15, on 08 July 2013 - 12:14 AM, said:

Answer: That was a typo it should be i + 1, correct?

No. You need to review Aho's broken implementation of bubble sort. You can read about it at baavgai's post The Real Bubble Sort. If you are going t to implement Bubble sort, use Iverson's correct and true version.

This post has been edited by Skydiver: 07 July 2013 - 09:36 PM

Was This Post Helpful? 0
  • +
  • -

#38 jordannn15  Icon User is offline

  • D.I.C Head

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

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

Posted 07 July 2013 - 10:09 PM

View PostSkydiver, on 07 July 2013 - 09:29 PM, said:

View Postjordannn15, on 08 July 2013 - 12:14 AM, said:

Answer: With line #2 my professor told us to do it this way. So basically copy the very last one we just used i.e. used - 1 to the next available index and then do used-- to get rid of it.


Check your class notes. I think you are missing something.

Let's pretend you start off with no movies and you added "Kickass". So you'll end up with:
movie[0] = { "Kickass" }


and used == 1.
Now if we called your remove(), you'll end up with:
movie[0] = { "Kickass" }
movie[1] = { "Kickass" }


and used == 0.

I doubt that you intended to make a copy of the item you are removing just so that you could remove it.

Anyway, you didn't answer my question about used == CAPACITY. Let's pretend CAPACITY was 3, and we added 3 movies. We'll have:
movie[0] = { "A New Hope" }
movie[1] = { "The Empire Strikes Back" }
movie[2] = { "Return of the Jedi" }


and used == 3.

If we called remove(), what would happen would be:
movie[0] = { "A New Hope" }
movie[1] = { "The Empire Strikes Back" }
movie[2] = { "Return of the Jedi" }
movie[3] = { "Return of the Jedi" }


and [il]used
== 2. Assuming that things didn't crash, you just wrote into [il]movie[3]
which is the 4th element, but you've only allocated space for 3 elements.


Okay so I looked over the lecture and saw my problem and have fixed it with this implementation:
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);

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


Was This Post Helpful? 0
  • +
  • -

#39 jordannn15  Icon User is offline

  • D.I.C Head

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

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

Posted 07 July 2013 - 10:29 PM

Okay, everything is working very well except for my LoadFromFile and SaveToFile functions. If I run the program once and have movies still in the array and I quit, then I run it a second time the console will be blank until I press return once or twice and then if I choose to display all movies to see what I had in last time there won't be anything there so can you help me figure this one out? What am I doing wrong?

Here is all my updated code:

//dvdLib.h

#ifndef DVDLIB_H
#define DVDLIB_H
#include <iostream>
#include <string>
#include <cstdlib>
#include "dvd.h"

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

	dvdLib(size_t u = 0);

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

	void SeeAllY(std::ostream& outs);
	void SeeAllG(std::ostream& outs);
	void SeeAllR(std::ostream& outs);

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

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

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

#endif



//dvdLib.cc

#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::SeeAllY(ostream& outs){
	int year;

	cout << "What year do you want?\n\n";
	cout << "Enter movie year ->   ";
	cin >> year;

	for(size_t i = 0; i < used; i++){
		if(movies[i].getYear() == year)
			outs << movies[i] << endl;
	}
}

void dvdLib::SeeAllG(ostream& outs){
	string genre;
	char junk;

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

	for(size_t i = 0; i < used; i++){
		if(movies[i].getGenre() == genre)
			outs << movies[i] << endl;
	}
}

void dvdLib::SeeAllR(ostream& outs){
	string rating;
	char junk;

	cout << "What ratings do you want?\n\n";
	cout << "Enter movie rating ->   ";
	cin.get(junk);
	getline(cin, rating);

	for(size_t i = 0; i < used; i++){
		if(movies[i].getRating() == rating)
			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);

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

void 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[j].getTitle() > movies[j + 1].getTitle()){
				tmp = movies[j];
				movies[j] = 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[j].getYear() > movies[j + 1].getYear()){
				tmp = movies[i];
				movies[j] = 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 - 1; j++){
			if(movies[j].getRating() > movies[j + 1].getRating()){
				tmp = movies[j];
				movies[j] = movies[j + 1];
				movies[j + 1] = tmp;
			}
		}
	}
}

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

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

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

	while(choice != 11){
		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. Search for movies of a certain year.\n";
		cout << "6. Search for movies of a certain genre.\n";
		cout << "7. Search for movies of a certain rating.\n";
		cout << "8. Display all movies by title.\n";
		cout << "9. Display all movies by year.\n";
		cout << "10. Display all movies by rating.\n";
		cout << "11. Quit.\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.SeeAllY(cout);
	else if(choice == 6)
		obj.SeeAllG(cout);
	else if(choice == 7)
		obj.SeeAllR(cout);
	else if(choice == 8){
		obj.sortT();
		obj.SeeAll(cout);
	}
	else if(choice == 9){
		obj.sortY();
		obj.SeeAll(cout);
	}
	else if(choice == 10){
		obj.sortR();
		obj.SeeAll(cout);
	}
	else if(choice == 11)
		cout << "Goodbye.\n";
	else
		cout << "Invalid choice, please pick 1 - 9.\n";
	}
}

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



//main.cc

#include <iostream>
#include <fstream>
#include <cstdlib>
#include "dvd.h"
#include "dvdLib.h"

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

	return 0;
}


This post has been edited by jordannn15: 08 July 2013 - 12:01 AM

Was This Post Helpful? 0
  • +
  • -

#40 jimblumberg  Icon User is offline

  • member icon


Reputation: 4067
  • View blog
  • Posts: 12,550
  • Joined: 25-December 09

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

Posted 07 July 2013 - 10:42 PM

Have you changed your dvd class since the first post? If not then this problem is probably in your dvd.output() and dvd.input() functions.

Jim
Was This Post Helpful? 0
  • +
  • -

#41 jordannn15  Icon User is offline

  • D.I.C Head

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

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

Posted 07 July 2013 - 11:00 PM

Oh sorry I didn't even think about those, here they are:

//dvd.h

#ifndef DVD_H
#define DVD_H
#include <iostream>
#include <string>

class dvd {
   public:
          void setTitle(std::string t);
          void setYear(int y);
          void setRunningTime(int rt);
          void setGenre(std::string g);
          void setRating(std::string r);

          std::istream& input(std::istream& ins);
          std::ostream& output(std::ostream& outs) const;

          std::string getTitle() const{return title;}
          int getYear() const{return year;}
          int getRunningTime() const{return runTime;}
          std::string getGenre() const{return genre;}
          std::string getRating() const{return rating;}

   private:
          int year;
          int runTime;
          std::string title;
          std::string genre;
          std::string rating;
};

std::istream& operator >>(std::istream& fin, dvd& d);
std::ostream& operator <<(std::ostream& fout, const dvd& d);

#endif



//dvd.cc

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

void dvd::setTitle(string t){
     title = t;
}

void dvd::setYear(int y){
     year = y;
}

void dvd::setRunningTime(int rt){
     runTime = rt;
}

void dvd::setGenre(string g){
     genre = g;
}

void dvd::setRating(string r){
     rating = r;
}

istream& dvd::input(istream& ins){
	char junk;
	if(ins == cin){
		cout << "Title:   ";
		cin.get(junk);
		getline(cin, title);

		cout << "Year:   ";
		cin >> year;

		cout << "Run Time:   ";
		cin >> runTime;

		cout << "Genre:   ";
		cin.get(junk);
		getline(cin, genre);

		cout << "Rating:   ";
		getline(cin, rating);
	}else{
		getline(ins, title);
		ins >> year;
		ins >> runTime;
		getline(ins, genre);
		getline(ins, rating);
	}
	return ins;
}

ostream& dvd::output(ostream& outs) const{
	if(outs == cout){
		cout << "Title:   ";
		cout << title << endl;

		cout << "Year:   ";
		cout << year << endl;

		cout << "Run Time:   ";
		cout << runTime << endl;

		cout << "Genre:   ";
		cout << genre << endl;

		cout << "Rating:   ";
		cout << rating << endl;
	}else{
		outs << "Title:   ";
		outs << title << endl;

		outs << "Year:   ";
		outs << year << endl;

		outs << "Run Time:   ";
		outs << runTime << endl;

		outs << "Genre:   ";
		outs << genre << endl;

		outs << "Rating:   ";
		outs << rating << endl;
	}

	return outs;
}

istream& operator >>(istream& fin, dvd& d){
         d.input(fin);
         return fin;
}

ostream& operator <<(ostream& fout, const dvd& d){
         d.output(fout);
         return fout;
}


This post has been edited by jordannn15: 08 July 2013 - 12:00 AM

Was This Post Helpful? 0
  • +
  • -

#42 David W  Icon User is offline

  • DIC supporter
  • member icon

Reputation: 281
  • View blog
  • Posts: 1,788
  • Joined: 20-September 08

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

Posted 08 July 2013 - 01:53 AM

View Postjordannn15, on 08 July 2013 - 01:09 AM, said:

...

Okay so I looked over the lecture and saw my problem and have fixed it with this implementation:
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);

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



But above ... you had this:

Quote

Yeah never mind lol sorry I don't have to remove a certain movie just the very last movie in the array. And plus I haven't learned destructors yet. Right now I'm working on getting my sorts to work properly would you mind helping me?


Which is it?

1. Do you want to pop the last record in an array? (just --used)

or ...

2. Do you wish to remove a record if it exists in the array ...
(and move any/all records above down, and over)?
(then --used)

The code for 1. is really quite simple, but I would suggest an enhancement something like ...

void dvdLib::remove()
{
	if( used != 0 ) // if you choose an unsigned type like size_t for 'used' //
	{
		std::cout << movies[used-1] << "\nOk to remove (y/n) ? " << std::flush;
		if( std::cin.get() == 'y' ) 
			--used;
		else
			std::cout << "\nNOT removed!" << std::endl;

		std::cin.sync(); // keep cin stream 'flushed as we go'
	}
	else std::cout << "\nEmpty ...\n";
}


This post has been edited by David W: 08 July 2013 - 02:12 AM

Was This Post Helpful? 0
  • +
  • -

#43 jordannn15  Icon User is offline

  • D.I.C Head

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

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

Posted 08 July 2013 - 07:52 AM

View PostDavid W, on 08 July 2013 - 01:53 AM, said:

View Postjordannn15, on 08 July 2013 - 01:09 AM, said:

...

Okay so I looked over the lecture and saw my problem and have fixed it with this implementation:
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);

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



But above ... you had this:

Quote

Yeah never mind lol sorry I don't have to remove a certain movie just the very last movie in the array. And plus I haven't learned destructors yet. Right now I'm working on getting my sorts to work properly would you mind helping me?


Which is it?

1. Do you want to pop the last record in an array? (just --used)

or ...

2. Do you wish to remove a record if it exists in the array ...
(and move any/all records above down, and over)?
(then --used)

The code for 1. is really quite simple, but I would suggest an enhancement something like ...

void dvdLib::remove()
{
	if( used != 0 ) // if you choose an unsigned type like size_t for 'used' //
	{
		std::cout << movies[used-1] << "\nOk to remove (y/n) ? " << std::flush;
		if( std::cin.get() == 'y' ) 
			--used;
		else
			std::cout << "\nNOT removed!" << std::endl;

		std::cin.sync(); // keep cin stream 'flushed as we go'
	}
	else std::cout << "\nEmpty ...\n";
}



Okay what he showed me in the lecture is to copy the index of whatever movie the user wants to remove to used i.e. the next available spot in the array then do used--;

As for doing stuff like std::cin.sync() and std::flush I haven't learned that yet and I would put that in my code but then I think my professor would be suspicious.
Was This Post Helpful? 0
  • +
  • -

#44 David W  Icon User is offline

  • DIC supporter
  • member icon

Reputation: 281
  • View blog
  • Posts: 1,788
  • Joined: 20-September 08

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

Posted 08 July 2013 - 09:30 AM

View Postjordannn15, on 08 July 2013 - 10:52 AM, said:

...

Okay what he showed me in the lecture is to copy the index of whatever movie the user wants to remove to used i.e. the next available spot in the array then do used--;

As for doing stuff like std::cin.sync() and std::flush I haven't learned that yet and I would put that in my code but then I think my professor would be suspicious.



> Okay what he showed me in the lecture is to copy the index of whatever movie the user wants to remove to used i.e. the next available spot in the array then do used--

Do you think that 'obscures' the one you wish to delete?

What I think he was telling you, that perhaps you misunderstood was to

0. find the index of the record you wish to remove (if it exists)

1. COPY the record you wish to remove
to the next free index spot at the end of the array
(provided that there was still room in the array ...
i.e for index 'size', where record is being moved to, and size < MAX_SIZE)

2. Copy down and over each record above the one you wish to remove
suppose j is index of record to remove and that j < size < MAX_SIZE
then:
   movies[size] = movies[j]; // copy record at index j to first spot open at end
   for( int i = j; i < size-1; ++ i)
   {
      movies[i] = movies[i+1]; // copy all above down one (and over)
   }
   -- size;



3. --size (ii.e. --used) to reflect the new top position in the array

But note:

"movies[size] = movies[j]; // copy record at index j to first spot open at end"

this above line is NOT really needed,
since, if you next were to add a record,
that info moved (above) the top index position is over-written and lost.
So, just moving records (above the one record to delete) down one index, is sufficient


> As for doing stuff like std::cin.sync() and std::flush I haven't learned that yet and I would put that in my code but then I think my professor would be suspicious.

Can't you look up these in a reference and quote that ref. (after you understand the info there?)
(As a student, the sooner you learn to use a ref to get relevant info of library functions, the sooner you will enjoy coding.)

One of the big (and unnecessarily big) problems of student coding in C++/C ...
is learning about the input stream (buffer)
and how to keep it ready for the the next input
so that
that input doesn't grab something
that was left over in the in stream buffer
by a previous read or read error.

The sooner a student learns how to deal with invalid (numeric) input
and how to keep the input stream buffer in the desired (most often a flushed) state ...
the sooner that student's coding ease will be ...
for many student problems.

This post has been edited by David W: 08 July 2013 - 09:38 AM

Was This Post Helpful? 0
  • +
  • -

#45 jimblumberg  Icon User is offline

  • member icon


Reputation: 4067
  • View blog
  • Posts: 12,550
  • Joined: 25-December 09

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

Posted 08 July 2013 - 09:58 AM

Your problem with the file not containing any information is because you are using a local instance of your dvdLib class in your menu() function. Because you do this you never add any information to the current class instance (the one you used in main()). Therefore any changes to this local instance are lost when you return back to main().

You don't want to create a local copy of your class in your menu() function!.

void dvdLib::menu(int& choice){
	dvdLib obj;  // This is a local instance that gets destroyed when this function returns to it's calling function. You don't want or need this!
	dvd item;
	string name;

	while(choice != 11){
		cout << "1. Insert a movie.\n";

		cout << "11. Quit.\n\n";
		cout << "Enter your choice ->   ";
		cin >> choice;

	if(choice == 1)
		//obj.insert(item); // You should be using the current class instance, not that local instance. Get rid of the (obj.) just use insert().
           insert(item); // By the way since you never use item in this function you don't need to have a parameter.






Jim
Was This Post Helpful? 0
  • +
  • -

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