Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

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

ID: 3581287 • Letter: C

Question

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

Explanation / Answer

#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 char

// actually the cin buffer has remaining from last cin, so getline take this 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 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;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Chat Now And Get Quote