CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 13 of 13

Thread: Homework Help

  1. #1
    Join Date
    Jul 2014
    Posts
    15

    Homework Help

    Hello, I was working on my C++ homework and was wondering if anyone could help me with part of it. I wrote the .cpp and .h files but now I am stuck. Here is what I am supposed to do:

    1. Write a subprogram (not a method) void promptForMovie(Movie & myMovie); that prompts the user for movie information, and returns a Movie object to the calling program. When this command is executed, the user will be asked to supply the attributes for the movie.
    2. Write a method void output(ostream & out); that will display movie information as follows:
    Movie:
    Director:
    Year:
    Rating:
    IMDB URL:

    Here is my header file: MovieClass.h

    // This is the header file that includes the interface of the Movie class

    Code:
    #ifndef _MOVIE_
    #define _MOVIE_
    
    #include <string>
    
    using namespace std;
    
    	enum Movie_Rating {G,PG,PG13,R,NC17,NR} ;
    
    	class Movie
    	{
    	  public: // interface of the Socialite_user class
    		  	
    		//------------ Constructors ------------------
    			
    			Movie();
    			Movie(const string& title) ;
    			Movie(const string& title, 
    			      const string& director, 
    			      Movie_Rating rating,
    			      unsigned int year,
    			      const string& path) ;
    		  	
    		// ------------------------------------------------------
    		// ----- Destructor -------------------------------------
    		// ------------------------------------------------------
    		
    		//	~Movie();  // To be implemented in a future assignment.
    	  	
    		//------------ Inspectors --------------------
    	  	
    			string getTitle() const ;
    			string getDirector() const ;
    			Movie_Rating getRating() const ;
    			unsigned int getYear() const ;
    			string getURL() const ;
    	  	
    		//------------ Mutators ---------------------
    
    			void setTitle(const string& title); 
    			void setDirector(const string& director) ;
    			void setRating(Movie_Rating rating)  ;
    			void setYear(unsigned int year)  ;
    			void setURL(const string& path)  ;
    
    		private: // private data fields associated with Socialite_user objects
    			// Underscores indicate a private member variable
    
    			string title_ ;
    			string director_ ;
    			Movie_Rating rating_ ;
    			unsigned int year_ ;
    			string  url_ ;
    	};
    
    #endif
    MovieClass.cpp :

    // This is the file that includes the implementation of the Movie class

    Code:
    #include "MovieClass.h"
    #include <string>
    #include <cstdlib>
    #include <ciso646>
    #include <sstream>
    
    using namespace std;
    
    
    //-----------Constructors-----------------
    
    Movie::Movie()
    {
    		title_ = "";
    		director_ = "";
    		url_ = "";
    		rating_;
    		year_;
    }
    
    Movie::Movie(const string& title)
    {
    	title_ = "";
    }
    
    Movie::Movie(const string& title, const string& director, Movie_Rating rating, unsigned int year, const string& path) 
    {
    		title_ = "";
    		director_ = "";
    		url_ = "";
    		rating_;
    		year_;
    }
    
    
    //---------------Inspectors--------------------------
    
    string Movie::getTitle() const
    {
    	return title_;
    }
    
    string Movie::getDirector() const
    { 
    	return director_;
    }
    
    Movie_Rating Movie::getRating() const
    {
    	return rating_;
    }
    
    unsigned int Movie::getYear() const
    {
    	return year_;
    }
    
    string Movie::getURL() const
    {
    	return url_;
    }
    
    //--------------------Mutators-------------------
    
    void Movie::setDirector(const string& director)
    {
    	director_ = director;
    }
    
    void Movie::setRating(Movie_Rating rating)
    {
    	rating_ = rating;
    }
    
    void Movie::setYear(unsigned int year)
    {
    	year_ = year;
    }
    
    void Movie::setURL(const string& path)
    {
    	url_ = path;
    }
    And this is supposed to be my test file; I am pretty sure I wrote this completely wrong. I think this is where I would put the subprogram and method they are asking for?: main.cpp

    Code:
    // This is the file that will test the implementation of the MovieClass
    
    #include <iostream>
    #include <string>
    #include <iomanip>
    #include <fstream>
    #include "MovieClass.h"
    
    using namespace std;
    
    //------------------------------------------------------
    //	Prints out Movie Information
    //------------------------------------------------------
    
    	void output(ostream & out);
    
    int main(void) 
    {
    	Movie promptForMovie;
    
    	cout << "Movie: " << Movie.setTitle << endl;
    	cout << "Director: " << Movie.setDirector << endl;
    	cout << "Year: " << Movie.setYear << endl;
    	cout << "Rating: " << Movie.setRating << endl;
    	cout << "IMDB URL: " << Movie.setURL << endl;
    	
    	ofstream myfile;
        myfile.open ("MovieTest.txt");
    
        myfile << "Movie: ";
        myfile << promptForMovie.getTitle();
        myfile << "\n";
    	myfile << "Director: ";
    	myfile << promptForMovie.getDirector();
    	myfile << "\n";
    	myfile << "Year: ";
    	myfile << promptForMovie.getYear();
    	myfile << "\n";
    	myfile << "Rating: ";
    	myfile << promptForMovie.getRating();
    	myfile << "\n";
    	myfile << "IMDB Url: ";
    	myfile << promptForMovie.getURL();
    	myfile << "\n";
    
        myfile.close();
        return 0;
    }
    Edit & Run


    Thank you
    Last edited by VictorN; July 7th, 2014 at 12:00 PM. Reason: Added CODE tags

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: Homework Help

    Quote Originally Posted by cloud125 View Post
    Hello, I was working on my C++ homework and was wondering if anyone could help me with part of it. I wrote the .cpp and .h files but now I am stuck. Here is what I am supposed to do:

    1. Write a subprogram (not a method) void promptForMovie(Movie & myMovie); that prompts the user for movie information, and returns a Movie object to the calling program. When this command is executed, the user will be asked to supply the attributes for the movie.
    2. Write a method void output(ostream & out); that will display movie information as follows:
    Movie:
    Director:
    Year:
    Rating:
    IMDB URL:

    Here is my header file: MovieClass.h
    ...
    First thing you should have done is using Code tags around code snippets. See Announcement: Before you post....Otherwise your code is absolutely unreadable.


    Quote Originally Posted by cloud125 View Post
    And this is supposed to be my test file; I am pretty sure I wrote this completely wrong. I think this is where I would put the subprogram and method they are asking for?: main.cpp...
    Well, not it is the time to use a debugger. It will help you to find out what where and why goes wrong or unexpected:
    http://msdn.microsoft.com/en-us/library/y740d9d3.aspx
    Victor Nijegorodov

  3. #3
    Join Date
    Jul 2014
    Posts
    15

    Re: Homework Help

    Is there a way to edit my post or delete it so I can fix it?

  4. #4
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: Homework Help

    You'll be able to edit your posts after sending about five ones.
    Well, I will now edit your OP.
    Victor Nijegorodov

  5. #5
    Join Date
    Jul 2014
    Posts
    15

    Re: Homework Help

    Okay, thank you for your help!

  6. #6
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: Homework Help

    1) Your constructors are not correct. For
    Code:
    Movie(const string& title) ;
    Movie(const string& title, const string& director, Movie_Rating rating, unsigned int year, const string& path) ;
    you don't actually set the private variables from the parameters. eg for just the title try
    Code:
    Movie::Movie(const string& title) : title_(title) {}
    2)
    Code:
    Movie promptForMovie;
    This creates an instance of the class Movie called promptForMovie. It doesn't do anything else. It doesn't ask the user to enter details of movies.

    3)
    Code:
    cout << "Movie: " << Movie.setTitle << endl;
    Movie is a class not a class instance so this statement is not correct. Also setTitle takes a parameter which you don't provide. To display the title element try
    Code:
    cout << "Movie: " << promptForMovie.GetTitle() << endl;
    4) If you want to set elements of the class then one way would be
    Code:
    cout << "Enter title: ";
    string mTitle;
    cin >> mTitle;
    promptForMovie.setTile(mTitle);
    etc etc for the other elements.

    According to your post #1, however, promptForMovie should be a function not a class instance. So why make it a class instance? You are given the function declaration and having the class definition it should be fairly easy to write the function body
    Code:
    void promptForMovie(Movie& myMovie)
    {
    cout << "Enter title: ";
    string mTitle;
    cin >> mTitle;
    myMovie.setTile(mTitle);
    //etc etc
    }
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  7. #7
    Join Date
    Jul 2014
    Posts
    15

    Re: Homework Help

    Quote Originally Posted by 2kaud View Post
    1) Your constructors are not correct. For
    Code:
    Movie(const string& title) ;
    Movie(const string& title, const string& director, Movie_Rating rating, unsigned int year, const string& path) ;
    you don't actually set the private variables from the parameters. eg for just the title try
    Code:
    Movie::Movie(const string& title) : title_(title) {}
    2)
    Code:
    Movie promptForMovie;
    This creates an instance of the class Movie called promptForMovie. It doesn't do anything else. It doesn't ask the user to enter details of movies.

    3)
    Code:
    cout << "Movie: " << Movie.setTitle << endl;
    Movie is a class not a class instance so this statement is not correct. Also setTitle takes a parameter which you don't provide. To display the title element try
    Code:
    cout << "Movie: " << promptForMovie.GetTitle() << endl;
    4) If you want to set elements of the class then one way would be
    Code:
    cout << "Enter title: ";
    string mTitle;
    cin >> mTitle;
    promptForMovie.setTile(mTitle);
    etc etc for the other elements.

    According to your post #1, however, promptForMovie should be a function not a class instance. So why make it a class instance? You are given the function declaration and having the class definition it should be fairly easy to write the function body
    Code:
    void promptForMovie(Movie& myMovie)
    {
    cout << "Enter title: ";
    string mTitle;
    cin >> mTitle;
    myMovie.setTile(mTitle);
    //etc etc
    }
    Thanks so much! This helps a lot. I will work on it right now using this information; if I have any more questions I'll be sure to ask. ^^

  8. #8
    Join Date
    Jul 2014
    Posts
    15

    Re: Homework Help

    Quote Originally Posted by 2kaud View Post
    1) Your constructors are not correct. For
    Code:
    Movie(const string& title) ;
    Movie(const string& title, const string& director, Movie_Rating rating, unsigned int year, const string& path) ;
    you don't actually set the private variables from the parameters. eg for just the title try
    Code:
    Movie::Movie(const string& title) : title_(title) {}
    2)
    Code:
    Movie promptForMovie;
    This creates an instance of the class Movie called promptForMovie. It doesn't do anything else. It doesn't ask the user to enter details of movies.

    3)
    Code:
    cout << "Movie: " << Movie.setTitle << endl;
    Movie is a class not a class instance so this statement is not correct. Also setTitle takes a parameter which you don't provide. To display the title element try
    Code:
    cout << "Movie: " << promptForMovie.GetTitle() << endl;
    4) If you want to set elements of the class then one way would be
    Code:
    cout << "Enter title: ";
    string mTitle;
    cin >> mTitle;
    promptForMovie.setTile(mTitle);
    etc etc for the other elements.

    According to your post #1, however, promptForMovie should be a function not a class instance. So why make it a class instance? You are given the function declaration and having the class definition it should be fairly easy to write the function body
    Code:
    void promptForMovie(Movie& myMovie)
    {
    cout << "Enter title: ";
    string mTitle;
    cin >> mTitle;
    myMovie.setTile(mTitle);
    //etc etc
    }
    Okay so I am stuck again....
    Is this part of the code supposed to go under the main function? Or do I just call it somehow in the main?
    Code:
    void promptForMovie(Movie& myMovie)
    {
    	cout << "Enter movie title: ";
    	string myTitle;
    	cin >> myTitle;
    	myMovie.setTitle(myTitle);
    
    	cout << "Enter director's name: ";
    	string myDirector;
    	cin >> myDirector;
    	myMovie.setDirector(myDirector);
    
    	cout << "Enter movie year: ";
    	int myYear;
    	cin >> myYear;
    	myMovie.setYear(myYear);
    
    	cout << "Enter movie rating: ";
    	Movie_Rating myRating;
    	cin >> myRating;
    	myMovie.setRating(myRating);
    
    	cout << "Enter IMDB URL: ";
    	string myURL;
    	cin >> myURL;
    	myMovie.setURL(myURL);
    
    	}
    Also, it does not allow me to cin >> myRating. Do you know why that is? And lastly how would I do the second part: Write a method void output(ostream & out); that will display movie information. This is what I have:
    Code:
    #include <iostream>
    #include <string>
    #include <iomanip>
    #include <fstream>
    #include "MovieClass.h"
    
    using namespace std;
    
    
    
    //------------------------------------------------------
    //	Prints out Movie Information
    //------------------------------------------------------
    
    	void output(ostream & out);
    
    	void promptForMovie(Movie& myMovie)
    {
    	cout << "Enter movie title: ";
    	string myTitle;
    	cin >> myTitle;
    	myMovie.setTitle(myTitle);
    
    	cout << "Enter director's name: ";
    	string myDirector;
    	cin >> myDirector;
    	myMovie.setDirector(myDirector);
    
    	cout << "Enter movie year: ";
    	int myYear;
    	cin >> myYear;
    	myMovie.setYear(myYear);
    
    	cout << "Enter movie rating: ";
    	Movie_Rating myRating;
    	cin >> myRating;
    	myMovie.setRating(myRating);
    
    	cout << "Enter IMDB URL: ";
    	string myURL;
    	cin >> myURL;
    	myMovie.setURL(myURL);
    
    	}
    	
    
    int main(void) 
    {
    
    	Movie promptForMovie;
    	
    	ofstream myfile;
        myfile.open ("MovieTest.txt");
    
        myfile << "Movie: ";
        myfile << promptForMovie.getTitle();
        myfile << "\n";
    	myfile << "Director: ";
    	myfile << promptForMovie.getDirector();
    	myfile << "\n";
    	myfile << "Year: ";
    	myfile << promptForMovie.getYear();
    	myfile << "\n";
    	myfile << "Rating: ";
    	myfile << promptForMovie.getRating();
    	myfile << "\n";
    	myfile << "IMDB Url: ";
    	myfile << promptForMovie.getURL();
    	myfile << "\n";
    
        myfile.close();
        return 0;
    }

  9. #9
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: Homework Help

    Also, it does not allow me to cin >> myRating. Do you know why that is?
    myRating is of type Movie_Rating which is an enum.

    promptForMovie is a function so you call it from main() like you would any other function.

    And lastly how would I do the second part: Write a method void output(ostream & out); that will display movie information.
    What bit don't you understand? All that is needed is to add a method output to the Movie class that when called displays the movie info to the specified stream.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  10. #10
    Join Date
    Jul 2014
    Posts
    15

    Re: Homework Help

    Quote Originally Posted by 2kaud View Post
    myRating is of type Movie_Rating which is an enum.

    promptForMovie is a function so you call it from main() like you would any other function.



    What bit don't you understand? All that is needed is to add a method output to the Movie class that when called displays the movie info to the specified stream.
    Sorry, I am kind of a beginner with programming so I am a little confused with how to write methods and such.

  11. #11
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: Homework Help

    Quote Originally Posted by cloud125 View Post
    Sorry, I am kind of a beginner with programming so I am a little confused with how to write methods and such.
    What don't you understand? The class Movie already has methods (class functions). All you need to do is add another one.

    See
    http://www.cplusplus.com/doc/tutorial/classes/
    http://www.learncpp.com/cpp-tutorial...class-members/
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  12. #12
    Join Date
    Jul 2014
    Posts
    15

    Re: Homework Help

    Quote Originally Posted by 2kaud View Post
    What don't you understand? The class Movie already has methods (class functions). All you need to do is add another one.

    See
    http://www.cplusplus.com/doc/tutorial/classes/
    http://www.learncpp.com/cpp-tutorial...class-members/
    Okay thank you, that helps a lot ^^
    When I put enum as the type, it says it is not allowed

    Code:
    cout << "Enter movie rating: ";
    	enum myRating;
    	cin >> myRating;
    	myMovie.setRating(myRating);

  13. #13
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: Homework Help

    Code:
    enum myRating;
    is not correct. It should be
    Code:
    Movie_Rating myRating;
    With stream extraction (>>) for myRating, the issue is that stream extraction doesn't know how to extract an element of type Movie_Rating. Either an overloaded function for stream extraction (>>) for type Movie_Rating should be supplied or some other known type should be used for input and then converted/cast to the required type. If you want the user to be able to input ratings in the form P, PG etc then this should probably be entered as a string and then converted to the appropriate Movie_Rating value.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured