BELOW IS WHAT I HAVE SO FAR...IT WILL COMPILE, BUT WHEN I DEBUG, IT SAYS THAT "YEAR" HAS NOT BEEN INITIALIZED. i HAVE TRIED EVERYTHING...BEEN THRU THE BOOK, TRIED GOOGLE-ING THE ANSWER...HARDEST CLASS I HAVE TAKEN WITH A PROFESSOR WHO DOES NOT EXPLAIN OR TEACH. DONT EVEN REALLY KNOW WHAT I AM DOING. HELP PLEASE?
The directions were to define a class named Movie. Include private fields for title, year and director. Include 2 public functions with the below prototypes. Include another function to display all information. Write a main()function that declars movie object named myFavoriteMovie. Set and display object's fields.

#include<iostream>
#include<string>
using namespace std;

class Movie
{
private:
        string Title;
        int Year;
        string Director;
public:
        void setTitle(string);
        void setYear(int);
        void setDirector(string);
        void display();
};

void Movie::setTitle(string movieTitle)
{
    Title = movieTitle;
}

void Movie::setYear(int num)
{
   while(Year < 2013)
       ++Year;
}

void Movie::setDirector(string Dir)
{
    Director = Dir;
}

void Movie::display()
{
    cout << Title << Year << Director << endl;

}

int main()
{
     Movie myFavoriteMovie;
     string Title;
     int Year;
     string Director;

    cout << Title << Year << Director << endl;

    myFavoriteMovie.setTitle(Title);
    myFavoriteMovie.setYear(Year);
    myFavoriteMovie.setDirector(Director);
    myFavoriteMovie.display();


    return 0;
}

Recommended Answers

All 2 Replies

Hint: Do it in your constructor.

A) CODE tags make your code easier to read.
B) What is setYear doing?!

void Movie::setYear(int num)
{
while(Year < 2013)//while year < 2013
++Year;//++year
//now year will be... 2013, regardless of num?
}

Perhaps this would work better?

Movie &Movie::setYear(int year)//I am going to return this, to allow for chaining
{
    this->year=year;
    if (this->year>2013)
    {
        this->year=0;//? I do not really know why the year cannot be greater than 2013
    }
    return *this;
}

In case my above code confused you... In every function of every class there exists an 'extra' parameter. This parameter looks like this: const className *this This parameter is a pointer to the class itself. Its methods can be accessed as usual from a pointer (using -> instead of .) and it can be returned by functions.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.