Click here to Skip to main content
15,891,744 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
#include <iostream>
#include <string>
using namespace std;

class Movie
{
private:
	string name;
	string director;
public:
	Movie()
	{
		name = "Movie";
		director = "Director";
	}
	Movie(string n, string d)
	{
		name = n;
		director = d;
	}
	void setName(string n)
	{
		name = n;
	}
	void setDirector(string d)
	{
		director = d;
	}
	string getName()
	{
		return name;
	}
	string getDirector()
	{
		return director;
	}
	void DisplayData(Movie data[], int size)
	{
		cout << "Movie list: ";
		for (int c = 0; c < size; c++)
		{
			cout << name[c] << " ";
		}

		cout << endl;
	}
	void BubbleSort(Movie data[], int size)
	{
		string temp;
		bool swap;
		do
		{
			swap = false;
			for (int c = 0; c < size - 1; c++)
			{
				if (data[c].getName() > data[c+1].getName())
				{
					temp = data[c];
				}
			}
		} while (swap = true);
	}

};


What I have tried:

I've tried different things but to no avail.
Posted
Updated 21-Mar-18 21:53pm
v2
Comments
PIEBALDconsult 21-Mar-18 22:56pm    
Look at the line number.

{
    temp = data[c];
}


You are trying to assign 'temp' (a string) to an instance of a Movie class. You need to assign it to a copy of the 'name' string in the class. You can do so by creating an '=' operator for the class, or through 'getName' or another way if you wish.
 
Share this answer
 
Comments
CPallini 22-Mar-18 3:35am    
5.
Maciej Los 22-Mar-18 6:06am    
5ed!
Derangedbuffalo 22-Mar-18 8:44am    
Tanks! I really appreciate it. I ended up using getName and it worked!
Your while statement is wrong:
do
		{
			swap = false;
			for (int c = 0; c < size - 1; c++)
			{
				if (data[c].getName() > data[c+1].getName())
				{
					temp = data[c];
				}
			}
		} while (swap = true);

You're assigning true to swap, not making a comparison. 'Equal to' is ==, not =.
 
Share this answer
 
Comments
CPallini 22-Mar-18 3:54am    
5.
Maciej Los 22-Mar-18 6:06am    
5ed!
Hey man, it is C++ and you far better alternatives to arrays and bubble sort.
C++
#include <vector>
#include <algorithm>
#include <iostream>
#include <string>
using namespace std;

class Movie
{
private:
  string name;
  string director;
public:

  Movie(string n="Movie", string d="Director"):name(n), director(d){}

  void setName(string n)
  {
    name = n;
  }
  void setDirector(string d)
  {
    director = d;
  }
  const   string getName() const
  {
    return name;
  }
  const string getDirector() const
  {
    return director;
  }
};

ostream & operator << (ostream & os, const Movie & movie)
{
  os << movie.getName();
  return os;
}

int main()
{
  vector< Movie > mv{ Movie("Foo", "boo"), Movie("Goo"), Movie("AAA", "BBB")};

  cout << "list of movies: ";
  for ( const auto & m : mv)
    cout << m << " ";
  cout << endl;

  sort( mv.begin(), mv.end(), [](const Movie & m1, const Movie & m2) {return (m1.getName() < m2.getName());});

  cout << "sorted list of movies: ";
  for ( const auto & m : mv)
    cout << m << " ";
  cout << endl;
}
 
Share this answer
 
Comments
Maciej Los 22-Mar-18 6:06am    
5ed!
CPallini 22-Mar-18 9:42am    
Thank you very much, Maciej!
Derangedbuffalo 22-Mar-18 8:30am    
Thanks man! I appreciate it. I know there are tons of other, better ways but this assignment calls for arrays and bubblesort. Not my cup of tea. Lol Thanks for the example.
CPallini 22-Mar-18 9:46am    
Well, you didn't stated that in the OP.
Even with arrays and bubble sort as requirements, you can improve your code. For instance BubbleSort and DisplayData should actually be static (instead of instance) methods.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900