The Following User Says Thank You to bandora For This Useful Post: | ||
![]() |
2013-11-26
, 06:46
|
|
Posts: 1,637 |
Thanked: 4,424 times |
Joined on Apr 2009
@ Germany
|
#2
|
![]() |
2013-11-26
, 07:29
|
|
Posts: 1,338 |
Thanked: 1,055 times |
Joined on Oct 2009
@ California, USA / Jordan
|
#3
|
![]() |
2013-11-26
, 08:52
|
|
Posts: 6,453 |
Thanked: 20,983 times |
Joined on Sep 2012
@ UK
|
#4
|
c = cin.get(); if (c != '\n') { cin.unget(); } while((c = cin.get()) != '\n') { setMovieName.push_back(c); }
![]() |
2013-11-26
, 09:41
|
|
Posts: 355 |
Thanked: 396 times |
Joined on Dec 2009
@ USA
|
#5
|
![]() |
2013-11-26
, 10:26
|
|
Posts: 6,453 |
Thanked: 20,983 times |
Joined on Sep 2012
@ UK
|
#6
|
![]() |
2013-11-26
, 20:06
|
|
Posts: 1,338 |
Thanked: 1,055 times |
Joined on Oct 2009
@ California, USA / Jordan
|
#7
|
#include <iostream> #include <string> using namespace std; class Movie { private: string movieName, MPAARating; int numRate[5]; public: Movie(string movie) { movieName = movie; MPAARating=5; numRate[0]=0; numRate[1]=0; numRate[2]=0; numRate[3]=0; numRate[4]=0; } Movie() // Default Constructor { MPAARating=5; numRate[0]=0; numRate[1]=0; numRate[2]=0; numRate[3]=0; numRate[4]=0; } string getMovieName() { return movieName; } void setMovieName(string str) { movieName = str; } string getMPAARating() { return MPAARating; } void setMPAARating(string str) { MPAARating = str; } void addRating(int num) { int i = num; if( num>0 && num <= 5) { this->numRate[i-1]++; } else { cout <<"Number out of range."<<endl; } } double getAverage() { double num = numRate[0]+numRate[1]+numRate[2]+numRate[3]+numRate[4]; double denom = 5.0; double result = num/denom; return result; } }; int main() { string setMovieName; int rating; char again = 'y'; cout << "Insert a movie name: "; char c = 0; c = cin.get(); if (c != '\n') { cin.unget(); } while((c = cin.get()) != '\n') { setMovieName.push_back(c); } Movie obj1(setMovieName); //char c = 0; //while(c !='\n') //{ // c = cin.get(); // setMovieName.push_back(c); //} // //setMovieName.erase(setMovieName.end() - 1); //setMovieName.erase(setMovieName.begin() + setMovieName.size() - 1); while (again == 'y' || again == 'Y') { cout << "Add the rating of the movie (1-5): "; cin >> rating; obj1.addRating(rating); cout << "Add more ratings? (y/n): "; cin >> again; } cout << "\nInsert a movie name: "; c = cin.get(); if (c != '\n') { cin.unget(); } while((c = cin.get()) != '\n') { setMovieName.push_back(c); } Movie obj2(setMovieName); //c = 0; //while(c !='\n') //{ // c = cin.get(); // setMovieName.push_back(c); //} // //setMovieName.erase(setMovieName.end() - 1); again = 'y'; while (again == 'y' || again == 'Y') { cout << "Add the rating of the movie (1-5): "; cin >> rating; obj2.addRating(rating); cout << "Add more ratings? (y/n): "; cin >> again; } double ave1 = obj1.getAverage(); double ave2 = obj2.getAverage(); cout <<"\nAverage rating for "<< obj1.getMovieName() <<": " <<ave1<<endl; cout <<"Average rating for "<< obj2.getMovieName() <<": " <<ave2<<endl; system("pause"); return 0; }
![]() |
2013-11-26
, 20:44
|
|
Posts: 6,453 |
Thanked: 20,983 times |
Joined on Sep 2012
@ UK
|
#8
|
loop read_movie_name loop read_rating ask "rate again?" if (answer_was_no) break loop end loop calculate_averages ask "another movie?" if (answer_was_no) break loop end loop
The Following User Says Thank You to pichlo For This Useful Post: | ||
![]() |
2013-11-26
, 22:32
|
|
Posts: 1,338 |
Thanked: 1,055 times |
Joined on Oct 2009
@ California, USA / Jordan
|
#9
|
![]() |
2013-11-27
, 08:48
|
|
Posts: 6,453 |
Thanked: 20,983 times |
Joined on Sep 2012
@ UK
|
#10
|
The Following User Says Thank You to pichlo For This Useful Post: | ||
In the past you guys have been extremely generous and helpful, not to mention very informative! And that's the reason why I keep on asking you guys for help! So I want to thank you guys in advance for your help!
Ok onto the problem... I am creating a program for my C++ class that asks for a movie name and a bunch of ratings twice.. Then displays the average rating for each movie.. I got the code to work but with one small issue.. It only takes the first string from the cin.. So like if someone enters "American Pie" for example it will only read it as American... Now I managed to go around the issue and by using a small while loop and that worked for the first movie but it messed up the second cin...
Here's the code so you can see what I'm talking about:
EDIT: I tried different things like using a different char (d instead of c) for the second movie.. I tried to even make another contructor called setMovieName2... It just does the same..
This is what's happening when I run the program:
FarahFa.com
Last edited by bandora; 2013-11-26 at 06:36.