0

I am currently learning C++ and I've encountered a problem with classes that I cannot seem to be able to find a solution to. If ever there is a specific topic I should cover that will teach me how to do it let me know but if it's a mistake I made I would like help solving it please.

I have a console application that when run asks you to input the name of a movie, the year it was released and it's rating. The code asks you the same questions for 3 movies and then prints the results. It's just a code I made while following tutorials so it has no real purpose.

I made a class called "Movie" which hold the user's answers and a function to create the instance but it doesn't seem to work. I will paste the code with examples to make it easier to explain.

Movie.h

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

using namespace std;

class Movie
{
    private:
        string movieName, movieRating;
        int movieYear;

    public:
        Movie();
        Movie(string, int, string);
        virtual ~Movie();

        string getName();
        string getRating();
        int getYear();

        void setName(string);
        void setRating(string);
        void setYear(int);

    protected:
};

#endif // MOVIE_H

Movie.cpp

#include "Movie.h"
#include <iostream>
#include <string>
#include <ctime>

using namespace std;

Movie::Movie()
{
    movieName = "NA";
    movieYear = 1999;
    movieRating = "NA";
}

Movie::Movie(string name, int year, string rating)
{
    movieName = name;

    time_t t = time(0);   // get time now
    struct tm * now = localtime( & t );
    int x = (now->tm_year + 1900);
    while(year > x || year < 1889)
    {
        cout<<"Please enter a valid year: ";
        cin>>year;
    }
    movieYear = year;

    movieRating = rating;
}

Movie::~Movie()
{

}


string Movie::getName()
{
    return movieName;
}

string Movie::getRating()
{
    return movieRating;
}

int Movie::getYear()
{
     return movieYear;
}

void Movie::setName(string n)
{
    movieName = n;
}

void Movie::setRating(string r)
{
    movieRating = r;
}

void Movie::setYear(int y)
{
    time_t t = time(0);   // get time now
    struct tm * now = localtime( & t );
    int x = (now->tm_year + 1900);
    while(y > x || y < 1889)
    {
        cout<<"Please enter a valid year: ";
        cin>>y;
    }
    movieYear = y;

}

main.cpp

#include <iostream>
#include <string>
#include "Movie.h"

using namespace std;

void newMovie(Movie);
void printMovies(Movie);

int main()
{
    Movie movie1("a", 1900, "a");
    Movie movie2("a", 1900, "a");
    Movie movie3("a", 1900, "a");

    newMovie(movie1);
    //newMovie(movie2);
    //newMovie(movie3);
    printMovies(movie1);
    //printMovies(movie2);
    //printMovies(movie3);
    return 0;
}

void newMovie(Movie movie)
{

    string inName, inRating;
    int inYear;
    cout<<"Enter a movie name: ";
    cin>>inName;
    movie.setName(inName);
    cout<<"Enter the year it was released: ";
    cin>>inYear;
    movie.setYear(inYear);
    cout<<"Enter the suggested rating: ";
    cin>>inRating;
    movie.setRating(inRating);
    cout<<endl<<endl;

}

void printMovies(Movie movie)
{
    cout<<"Name: "<<movie.getName()
        <<"\nYear: "<<movie.getYear()
        <<"\nRating: "<<movie.getRating()
        <<endl<<endl;
}

Now what happens when i run this is that the movie gets printed like this:

Enter a movie name: test Enter the year it was released: 2001 Enter the suggested rating: PG Name: a Year: 1900 Rating: a Process returned 0 (0x0) execution time : 6.912 s Press any key to continue.

It seems like the change made in newMovie(Movie movie) does not stay. If i do a print in the middle of it by doing something like this:

void newMovie(Movie movie)
{

    string inName, inRating;
    int inYear;
    cout<<"Enter a movie name: ";
    cin>>inName;
    movie.setName(inName);
    cout<<"Enter the year it was released: ";
    cin>>inYear;
    movie.setYear(inYear);
    cout<<"Enter the suggested rating: ";
    cin>>inRating;
    movie.setRating(inRating);
    cout<<endl<<endl;

    cout<<movie.getName();

}

I do obtain the right name when printing which means that the class works fine. The only works inside the function as if it was private and I can't figure out why.

Blanco
  • 1
  • 2
    `void newMovie(Movie &movie)` - you need to pass in the Movie object by reference, not value, otherwise all your changes disappear at the end of the call, as you have seen. – Ken Y-N Nov 21 '16 at 00:34
  • And a [fuller fuller answer here](http://stackoverflow.com/a/373455/3943312). – Sam Varshavchik Nov 21 '16 at 00:38
  • I get an error saying " undefined reference to `newMovie(Movie)' " on line 16 which is in the main() "newMovie(movie1);" – Blanco Nov 21 '16 at 04:49
  • I actualy did some research on passing by reference and it did solve the problem. Thanks a lot! – Blanco Nov 21 '16 at 04:55

0 Answers0