Question

C++ Language I have a class named movie which allows a movie object to take the...

C++ Language

I have a class named movie which allows a movie object to take the name, movie and rating of a movie that the user inputs. Everything works fine but when the user enters a space in the movie's name, the next function that is called loops infinetly. I can't find the source of the problem. Down below are my .cpp and .h file for the program.

#include <iostream>
#include "movie.h"
using namespace std;

movie::movie() {
movieName = "Not Named Yet"; movieYear = 0; movieRating = 0;
}

void movie::setMovieName() {
cout << "Movie name ?" << endl;
getline(cin, movieName);
}

string movie::getMovieName() {
return movieName;
}

void movie::setMovieYear() {
int y;
cout << "What is the year the movie was released?" << endl;
cin >> y;
  
while (y < 1896 || y > 2016) {
cout << "Please enter a valid year?" << endl;
cin >> y;
}
movieYear = y;
}

int movie::getMovieYear() {
return movieYear;
}

void movie::setRating() {
float x;
cout << "What is the movie rating from 1-100(1 being lowest, 100 being highest)" << endl;
cin >> x;
  
while (x < 1 || x > 100) {
cout << "Please enter a valid rating." << endl;
cin >> x;
}
movieRating = x;
}

float movie::getRating() {
return movieRating;
}

int main() {
cout << "What is the name, year, and rating of your three favorite movies?" << endl;
movie m1;
m1.setMovieName();
m1.setMovieYear();
m1.setRating();
  
cout << "-------On to the next movie---------" << endl;
movie m2;
m2.setMovieName();
m2.setMovieYear();
m2.setRating();
  
cout << "-----------Final movie -------------" << endl;
movie m3;
m3.setMovieName();
m3.setMovieYear();
m3.setRating();
  
cout << "Your three favorite movies are: " << m1.getMovieName() << ", " << m2.getMovieName() << ", " << m3.getMovieName() << "." << endl;
cout << "The year they were released are:" << m1.getMovieYear() << ", " << m2.getMovieYear() << ", " << m3.getMovieYear() << "." << endl;
cout << "Their movie ratings are: " << m1.getRating() << " , " << m2.getRating() << " , " << m3.getRating() << "." << endl;
}

/////////////////////////////.h file//////////////////////////////////////////////////////////

#ifndef MOVIE_H
#define MOVIE_H
#include <iostream>
using namespace std;

class movie {
public:
movie();
void setMovieName();
string getMovieName();
void setMovieYear();
int getMovieYear();
void setRating();
float getRating();
  
  
  
  
  
private:
string movieName;
int movieYear;
float movieRating;
  
  
};
#endif

//////////////////////////// .h file///////////////////////////

#ifndef MOVIE_H
#define MOVIE_H
#include <iostream>
using namespace std;

class movie {
public:
movie();
void setMovieName();
string getMovieName();
void setMovieYear();
int getMovieYear();
void setRating();
float getRating();
  
  
  
  
  
private:
string movieName;
int movieYear;
float movieRating;
  
  
};
#endif

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include <iostream>
#include "movie.h"
using namespace std;
movie::movie() {
movieName = "Not Named Yet"; movieYear = 0; movieRating = 0;
}
void movie::setMovieName() {
cout << "Movie name ?" << endl;
getline(cin, movieName);
}

string movie::getMovieName() {
return movieName;
}
void movie::setMovieYear() {
int y;
cout << "What is the year the movie was released?" << endl;
cin >> y;

while (y < 1896 || y > 2016) {
cout << "Please enter a valid year?" << endl;
cin >> y;
}
movieYear = y;
}
int movie::getMovieYear() {
return movieYear;
}
void movie::setRating() {
float x;
cout << "What is the movie rating from 1-100(1 being lowest, 100 being highest)" << endl;
cin >> x;

while (x < 1 || x > 100) {
cout << "Please enter a valid rating." << endl;
cin >> x;
}
movieRating = x;
}
float movie::getRating() {
return movieRating;
}
int main() {
cout << "What is the name, year, and rating of your three favorite movies?" << endl;
movie m1;
m1.setMovieName();
m1.setMovieYear();
m1.setRating();

cout << "-------On to the next movie---------" << endl;
movie m2;
cin.ignore();// use this cin.ignore() it ignores the \n char

// actually the cin buffer has \n remaining from last cin, so getline take this \n character and gets terminated; ignore
//the content of buffer before getline

m2.setMovieName();
m2.setMovieYear();
m2.setRating();

cout << "-----------Final movie -------------" << endl;
movie m3;
cin.ignore();// use this cin.ignore() it ignores the \n char

m3.setMovieName();
m3.setMovieYear();
m3.setRating();

cout << "Your three favorite movies are: " << m1.getMovieName() << ", " << m2.getMovieName() << ", " << m3.getMovieName() << "." << endl;
cout << "The year they were released are:" << m1.getMovieYear() << ", " << m2.getMovieYear() << ", " << m3.getMovieYear() << "." << endl;
cout << "Their movie ratings are: " << m1.getRating() << " , " << m2.getRating() << " , " << m3.getRating() << "." << endl;
}

Add a comment
Know the answer?
Add Answer to:
C++ Language I have a class named movie which allows a movie object to take the...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Coding in c++

    I keep getting errors and i am so confused can someone please help and show the input and output ive tried so many times cant seem to get it. main.cpp #include <iostream> #include <vector> #include <string> #include "functions.h" int main() {    char option;    vector<movie> movies;     while (true)     {         printMenu();         cin >> option;         cin.ignore();         switch (option)         {            case 'A':            {                string nm;                int year;                string genre;                cout << "Movie Name: ";                getline(cin, nm);                cout << "Year: ";                cin >> year;                cout << "Genre: ";                cin >> genre;                               //call you addMovie() here                addMovie(nm, year, genre, &movies);                cout << "Added " << nm << " to the catalog" << endl;                break;            }            case 'R':            {                   string mn;                cout << "Movie Name:";                getline(cin, mn);                bool found;                //call you removeMovie() here                found = removeMovie(mn, &movies);                if (found == false)                    cout << "Cannot find " << mn << endl;                else                    cout << "Removed " << mn << " from catalog" << endl;                break;            }            case 'O':            {                string mn;                cout << "Movie Name: ";                getline(cin, mn);                cout << endl;                //call you movieInfo function here                movieInfo(mn, movies);                break;            }            case 'C':            {                cout << "There are " << movies.size() << " movies in the catalog" << endl;                 // Call the printCatalog function here                 printCatalog(movies);                break;            }            case 'F':            {                string inputFile;                bool isOpen;                cin >> inputFile;                cout << "Reading catalog info from " << inputFile << endl;                //call you readFromFile() in here                isOpen = readFile(inputFile, &movies);                if (isOpen == false)                    cout << "File not found" << endl;...

  • BACKGROUND Movie Review sites collect reviews and will often provide some sort of average review to...

    BACKGROUND Movie Review sites collect reviews and will often provide some sort of average review to sort movies by their quality. In this assignment, you will collect a list of movies and then a list of reviews for each movie. You will then take a simple average (total of reviews divided by number of reviews) for each movie and output a sorted list of movies with their average review. Here you are provided the shell of a program and asked...

  • WONT COMPILE ERROR STRAY / TEXT.H AND CPP PROGRAM SAYING NOT DECLARED HAVE A DRIVER WILL...

    WONT COMPILE ERROR STRAY / TEXT.H AND CPP PROGRAM SAYING NOT DECLARED HAVE A DRIVER WILL PASTE AT BOTTOM MOVIES.CPP #include "movies.h" #include "Movie.h" Movies *Movies::createMovies(int max) { //dynamically create a new Movies structure Movies *myMovies = new Movies; myMovies->maxMovies = max; myMovies->numMovies = 0; //dynamically create the array that will hold the movies myMovies->moviesArray = new Movie *[max]; return myMovies; } void Movies::resizeMovieArray() { int max = maxMovies * 2; //increase size by 2 //make an array that is...

  • Write a C++ Program. You have a following class as a header file (dayType.h) and main()....

    Write a C++ Program. You have a following class as a header file (dayType.h) and main(). #ifndef H_dayType #define H_dayType #include <string> using namespace std; class dayType { public:     static string weekDays[7];     void print() const;     string nextDay() const;     string prevDay() const;     void addDay(int nDays);     void setDay(string d);     string getDay() const;     dayType();     dayType(string d); private:     string weekDay; }; #endif /* // Name: Your Name // ID: Your ID */ #include <iostream>...

  • Greetings, everybody I already started working in this program. I just need someone to help me...

    Greetings, everybody I already started working in this program. I just need someone to help me complete this part of the assignment (my program has 4 parts of code: main.cpp; Student.cpp; Student.h; StudentGrades.cpp; StudentGrades.h) Just Modify only the StudentGrades.h and StudentGrades.cpp files to correct all defect you will need to provide implementation for the copy constructor, assignment operator, and destructor for the StudentGrades class. Here are my files: (1) Main.cpp #include <iostream> #include <string> #include "StudentGrades.h" using namespace std; int...

  • Need this in C++ Goals: Your task is to implement a binary search tree of linked...

    Need this in C++ Goals: Your task is to implement a binary search tree of linked lists of movies. Tree nodes will contain a letter of the alphabet and a linked list. The linked list will be an alphabetically sorted list of movies which start with that letter. MovieTree() ➔ Constructor: Initialize any member variables of the class to default ~MovieTree() ➔ Destructor: Free all memory that was allocated void printMovieInventory() ➔ Print every movie in the data structure in...

  • Requirements I have already build a hpp file for the class architecture, and your job is to imple...

    Requirements I have already build a hpp file for the class architecture, and your job is to implement the specific functions. In order to prevent name conflicts, I have already declared a namespace for payment system. There will be some more details: // username should be a combination of letters and numbers and the length should be in [6,20] // correct: ["Alice1995", "Heart2you", "love2you", "5201314"] // incorrect: ["222@_@222", "12306", "abc12?"] std::string username; // password should be a combination of letters...

  • Need to implement Account.cpp and AccountManager.cpp code //Account.hpp #ifndef _ACCOUNT_HPP_ #define _ACCOUNT_HPP_ #include <string> using std::string;...

    Need to implement Account.cpp and AccountManager.cpp code //Account.hpp #ifndef _ACCOUNT_HPP_ #define _ACCOUNT_HPP_ #include <string> using std::string; class Account { public: Account(); Account(string, double); void deposit(double); bool withdraw(double); string getName() const; double getBalance() const; private: string name; double balance; }; #endif ////////////////////////////////////////////// //AccountManager.hpp #ifndef _ACCOUNT_MANAGER_HPP_ #define _ACCOUNT_MANAGER_HPP_ #include "Account.hpp" #include <string> using std::string; class AccountManager { public: AccountManager(); AccountManager(const AccountManager&); //copy constructor void open(string); void close(string); void depositByName(string,double); bool withdrawByName(string,double); double getBalanceByName(string); Account getAccountByName(string); void openSuperVipAccount(Account&); void closeSuperVipAccount(); bool getBalanceOfSuperVipAccount(double&) const;...

  • -------c++-------- The Passport class takes a social security number and the location of a photo file. The Passport class has a Photo class member, which in a full-blown implementation would store the...

    -------c++-------- The Passport class takes a social security number and the location of a photo file. The Passport class has a Photo class member, which in a full-blown implementation would store the passport photo that belongs in that particular passport. This Photo class holds its location or file name and the pixel data of the image. In this lab the pixel data is only used to show how memory maybe used when a program uses large objects. We will not...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT