Class with constructors and set/gets

Hello I am confused on why my set function doesn't work.

#include <iostream>
#include <string>
#include <cstdlib>

using namespace std;

class MovieData
{
private:
string title;
string director;
int year;
int time;

public:
MovieData();
void setTitle(string Title);
void setDirector(string Director);
void setYear(int Year);
void setTime(int Time);
string getTitle();
string getDirector();
int getYear();
int getTime();

};

//Nullify the variables
MovieData::MovieData()
{
title = "";
director = "";
year = 0;
time = 0;
}

void MovieData::setTitle(string Title)
{
title = Title;
}

void MovieData::setDirector(string Director)
{
director = Director;
}

void MovieData::setYear(int Year)
{
if (Year >= 1900 && Year <= 2004)
{
year = Year;
}
else
{
cout << "The year must be between 1900 and 2004." << endl;
year = 0;
}
}

void MovieData::setTime(int Time)
{
if (Time >= 0 && Time <= 14400)
{
time = Time;
}
if (Time < 0)
{
cout << "The time cannot be negative. Please enter a positive number"
<< endl;
time = 0;
}
}

string MovieData::getTitle()
{
return title;
}

string MovieData::getDirector()
{
return director;
}

int MovieData::getYear()
{
return year;
}

int MovieData::getTime()
{
return time;
}

int main()
{
MovieData setMovie;
MovieData getMovie;
string Title;
string Director;
int Year;
int Time;

cout << "Please enter the Title of the movie." << endl;
getline(cin,Title);

cout << "Please enter the Director's name." << endl;
getline(cin,Director);

cout << "Please enter the year it was released." << endl;
cin >> Year;

cout << "Please enter the amount of minutes the movie takes." << endl;
cin >> Time;

setMovie.setTitle(Title);
setMovie.setDirector(Director);
setMovie.setYear(Year);
setMovie.setTime(Time);

cout << "The title of the Movie is: " << getMovie.getTitle() << endl;
cout << "The Director's name is: " << getMovie.getDirector() << endl;
cout << "The year it was released was: " << getMovie.getYear() << endl;
cout << "The amount of minute the movie lasted was: "
<< getMovie.getTime() << endl;

return 0;
}

My output is:

Please enter the Title of the movie.
booby butt
Please enter the Director's name.
stupid noob
Please enter the year it was released.
1930
Please enter the amount of minutes the movie takes.
1337
The title of the Movie is:
The Director's name is:
The year it was released was: 0
The amount of minute the movie lasted was: 0

Process returned 0 (0x0) execution time : 24.037 s
Press any key to continue.
1
2
3
4
5
6
7
8
9
10
setMovie.setTitle(Title);
setMovie.setDirector(Director);
setMovie.setYear(Year);
setMovie.setTime(Time);

cout << "The title of the Movie is: " << getMovie.getTitle() << endl;
cout << "The Director's name is: " << getMovie.getDirector() << endl;
cout << "The year it was released was: " << getMovie.getYear() << endl;
cout << "The amount of minute the movie lasted was: "
     << getMovie.getTime() << endl;

You set the values for your setMovie object, but then you print the values of getMovie.

Those two are completely separate objects....
Well I was told to make two class objectives...
Maybe they meant for two different movies.

1
2
3
4
5
6
7
8
9
10
Movie movie1 , movie2;

movie1.setTitle( "Movie1" );
movie2.setTitle( "Movie2" );

//ect...


std::cout << movie1.getTitle() << std::endl;
//ect... 


btw what's up with this?

Please enter the Title of the movie.
booby butt
Please enter the Director's name.
stupid noob
Please enter the year it was released.
1930
Please enter the amount of minutes the movie takes.
1337
Last edited on
Topic archived. No new replies allowed.