-
July 7th, 2014, 10:15 AM
#1
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
-
July 7th, 2014, 11:23 AM
#2
Re: Homework Help
Originally Posted by cloud125
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.
Originally Posted by cloud125
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
-
July 7th, 2014, 11:45 AM
#3
Re: Homework Help
Is there a way to edit my post or delete it so I can fix it?
-
July 7th, 2014, 11:57 AM
#4
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
-
July 7th, 2014, 12:04 PM
#5
Re: Homework Help
Okay, thank you for your help!
-
July 7th, 2014, 02:37 PM
#6
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)
-
July 7th, 2014, 06:39 PM
#7
Re: Homework Help
Originally Posted by 2kaud
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. ^^
-
July 8th, 2014, 08:08 AM
#8
Re: Homework Help
Originally Posted by 2kaud
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;
}
-
July 8th, 2014, 12:21 PM
#9
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)
-
July 8th, 2014, 05:53 PM
#10
Re: Homework Help
Originally Posted by 2kaud
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.
-
July 9th, 2014, 04:13 AM
#11
Re: Homework Help
Originally Posted by cloud125
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)
-
July 9th, 2014, 06:50 AM
#12
Re: Homework Help
Originally Posted by 2kaud
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);
-
July 9th, 2014, 07:09 AM
#13
Re: Homework Help
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|