using if statements using class and ==

closed account (S23vqMoL)
hi, again. im stuck on printing the user's movie info input. I need to make it so that when the enter the title it will run the line of codes such as:
1
2
3
4
5
        movie1.setTitle("The Dark Knight");
	movie1.setDirector("Christopher Nolan");
	movie1.setYear(2008);
	movie1.setRating(PG13);
	movie1.setURL("http://www.imdb.com/title/tt0468569/");


i tried writing an else if statement like
if (movie1 == "The Dark Knight") to run these specific codes above but
i kept getting errors on the == sign.

helps pls :/

The whole program:
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
#include <iostream>
#include <string>
#include "Movie.h"

using namespace std;



void promptForMovie(Movie & myMovie);
// prompts the user the user for movie title and returns a Movie object to the calling program
// @param myMovie - takes the movie attribute that the user entered

int main () {
	Movie movie1;

	promptForMovie(movie1);

	if (movie1 == "The Dark Knight") {
	   movie1.setTitle("The Dark Knight");
	   movie1.setDirector("Christopher Nolan");
	   movie1.setYear(2008);
	   movie1.setRating(PG13);
	   movie1.setURL("http://www.imdb.com/title/tt0468569/");
	}

	else if(movie1 == "Django Unchained") {
		movie1.setTitle("Django Unchained");
		movie1.setDirector("Quentin Tarantino");
		movie1.setYear(2012);
		movie1.setRating(R);
		movie1.setURL("http://www.imdb.com/title/tt1853728/");
	}
	
	else if(movie1 == "Maleficent") {
		movie1.setTitle("Maleficent");
	    movie1.setDirector("Robert Stromberg");
	    movie1.setYear(2014);
		movie1.setRating(PG);
		movie1.setURL("http://www.imdb.com/title/tt1587310/");
	}
	
	else if (movie1 == "The Lion King") {
		movie1.setTitle("The Lion King");
		movie1.setDirector("Roger Allers, Rob Minkoff");
		movie1.setYear(1994);
		movie1.setRating(G);
		movie1.setURL("http://www.imdb.com/title/tt0110357/");
	}
	
	else if(movie1 == "American Psycho") {
		movie1.setTitle("American Psycho");
		movie1.setDirector("Mary Harron");
		movie1.setYear(2000);
		movie1.setRating(NC17);
		movie1.setURL("http://www.imdb.com/title/tt0144084/");
	}
       
        cout << movie1.toString() << endl;

	return 0;
}

void promptForMovie(Movie & myMovie) {
	cout << "Enter movie title: ";
	string mInfo;
	getline(cin, mInfo);
}
Last edited on
This stuff name is OPERATOR OVERLOADING. Suppose the var where title is saved is named "title" (std::string). Then, on the class:
[code]
friend bool operator==(Movie movie, std::string title)
{
return movie.title == title;
}
closed account (S23vqMoL)
i don't understand. can you elaborate more on that?
nuclearpenguin wrote:
hi, again. im stuck on printing the user's movie info input. I need to make it so that when the enter the title it will run the line of codes such as:


1
2
3
4
5
        movie1.setTitle("The Dark Knight");
	movie1.setDirector("Christopher Nolan");
	movie1.setYear(2008);
	movie1.setRating(PG13);
	movie1.setURL("http://www.imdb.com/title/tt0468569/");


nuclearpenguin wrote:
i tried writing an else if statement like
1
2
if (movie1 == "The Dark Knight") to run these specific codes above but
i kept getting errors on the == sign.

nuclearpenguin wrote:
helps pls :/

The whole program:


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
#include <iostream>
#include <string>
#include "Movie.h"

using namespace std;



void promptForMovie(Movie & myMovie);
// prompts the user the user for movie title and returns a Movie object to the calling program
// @param myMovie - takes the movie attribute that the user entered

int main () {
	Movie movie1;

	promptForMovie(movie1);

	if (movie1 == "The Dark Knight") {
	   movie1.setTitle("The Dark Knight");
	   movie1.setDirector("Christopher Nolan");
	   movie1.setYear(2008);
	   movie1.setRating(PG13);
	   movie1.setURL("http://www.imdb.com/title/tt0468569/");
	}

	else if(movie1 == "Django Unchained") {
		movie1.setTitle("Django Unchained");
		movie1.setDirector("Quentin Tarantino");
		movie1.setYear(2012);
		movie1.setRating(R);
		movie1.setURL("http://www.imdb.com/title/tt1853728/");
	}
	
	else if(movie1 == "Maleficent") {
		movie1.setTitle("Maleficent");
	    movie1.setDirector("Robert Stromberg");
	    movie1.setYear(2014);
		movie1.setRating(PG);
		movie1.setURL("http://www.imdb.com/title/tt1587310/");
	}
	
	else if (movie1 == "The Lion King") {
		movie1.setTitle("The Lion King");
		movie1.setDirector("Roger Allers, Rob Minkoff");
		movie1.setYear(1994);
		movie1.setRating(G);
		movie1.setURL("http://www.imdb.com/title/tt0110357/");
	}
	
	else if(movie1 == "American Psycho") {
		movie1.setTitle("American Psycho");
		movie1.setDirector("Mary Harron");
		movie1.setYear(2000);
		movie1.setRating(NC17);
		movie1.setURL("http://www.imdb.com/title/tt0144084/");
	}
       
        cout << movie1.toString() << endl;

	return 0;
}

void promptForMovie(Movie & myMovie) {
	cout << "Enter movie title: ";
	string mInfo;
	getline(cin, mInfo);
}


Just in case the original post gets deleted.
Last edited on
closed account (S23vqMoL)
^why would it get deleted?
Because you did that once already.
http://www.cplusplus.com/forum/general/137394/

Last edited on
closed account (S23vqMoL)
i did it so that a solved thread wouldnt have to keep being on the first page so that other ppls question would be answered :/
Editing out the content of your post doesn't drop the thread further down the list. It just prevents someone else in this community with a similar problem from benefiting from the answers/info given.

Edit - I think you have the option as the thread starter to mark a thread as solved so it gets the checkmark next to it on the main page of the forum.
Last edited on
closed account (S23vqMoL)
oh ok then. sry about that. i wont do it again :)
closed account (S23vqMoL)
srsly though does anyone have any suggestion on how i should do this?
You are using == to try to compare a Movie to string, and you haven't said how that is supposed to work. You can solve the problem by defining that comparing a Movie and a string amounts to comparing the title and the string, as iQChange suggested (read up on operator overloading), or you could just get the title from the movie and compare to the string you have (consider you have defined setters, you have getters, too, I assume).
Topic archived. No new replies allowed.