Hello gongong,
My apologies. When I say the (input.h) I missed the double quotes and my brain went elsewhere.
In that case and since it is a header file that you wrote, please include it with your program so everyone can see if there are any problems in it. Also it allows your program to compile properly.
This is your program that I have made some changes to, but mostly what you started with:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197
|
#include <cctype> //<--- Added.
#include <iostream>
#include <iomanip> //<--- Added.
#include <limits> //<--- Added.
#include <string>
//#include "input.h"
using namespace std; // <--- Best not to use.
// A recent post that is worth reading. http://www.cplusplus.com/forum/beginner/258335/
class Movie
{
private:
string name,
MPAA_Rating;
int ratedTerrible, //1
ratedBad, //2
ratedOK, //3
ratedGood, //4
ratedGreat; //5
public:
Movie();
Movie(string name, string MPAA_Rating);
string getName()const;
void setName(string name);
string getMPAA_Rating()const;
void setMPAA_Rating(string MPAA_Rating);
void addRating(int r);
double getAverage()const;
};
Movie::Movie() :
ratedTerrible(0),
ratedBad(0),
ratedOK(0),
ratedGood(0),
ratedGreat(0),
name(""),
MPAA_Rating("") {};
Movie::Movie(string name, string MPAA_Rating)
{
this->name = name;
this->MPAA_Rating = MPAA_Rating;
}
void Movie::setName(string name)
{
this->name = name;
}
string Movie::getName()const
{
return name;
}
void Movie::setMPAA_Rating(string MPAA_Rating)
{
if (MPAA_Rating == "G" ||
MPAA_Rating == "PG" ||
MPAA_Rating == "PG-13" ||
MPAA_Rating == "R")
this->MPAA_Rating = MPAA_Rating;
else
this->MPAA_Rating = "UNRATED";
}
string Movie::getMPAA_Rating()const
{
return MPAA_Rating;
}
void Movie::addRating(int r)
{
if (r == 1)
ratedTerrible = 0;
else if (r == 2)
ratedBad = 25;
else if (r == 3)
ratedOK = 50;
else if (r == 4)
ratedGood = 75;
else if (r == 5)
ratedGreat = 100;
else
return;
}
double Movie::getAverage()const
{
double average = 0;
return -1;
}
// <--- Added these prototypes.
char inputChar(string prompt, char yes, char no);
int inputInteger(string prompt, int startRange, int endRange);
string toUpper(string str);
int main()
{
Movie movieRatings;
string movieTitle, mpaaRating;
cout << "\n Enter a movie title: ";
std::getline(cin, movieTitle); //<--- Changed.
movieRatings.setName(movieTitle);
cout << "\n Enter MPAA Rating (G || PG || PG-13 || R): ";
std::getline(cin, mpaaRating); //<--- Changed.
movieRatings.setMPAA_Rating(toUpper(mpaaRating));
char ans;
do
{
int movieRating = inputInteger("\n Enter rating (1-Terrible, 2-Bad, 3-OK, 4-Good, 5-Great): ", 1, 5);
movieRatings.addRating(movieRating);
cout << "\n\n You rated " << movieTitle << " a " << movieRating << endl;
ans = inputChar("\n\n Add another rating (Y/N)? ", 'y', 'n');
} while (ans == 'y');
//output Changed the output.
cout << "\n"
<< std::setw(14) << "TITLE: " << movieRatings.getName() << '\n'
<< std::setw(14) << "MPAA RATING: " << movieRatings.getMPAA_Rating() << '\n'
<< std::setw(14) << "RATING: " << movieRatings.getAverage() << std::endl;
return 0;
}
//just some validation function that you may want to move up before main if you wish to run the code...
char inputChar(string prompt, char yes, char no)
{
char input;
do
{
cout << prompt;
if (!(cin >> input))
{
cout << "\n ERROR-1A: Invalid input. Must be a character type.\n";
cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); // <--- Requires header file <limits>.
}
//else if (tolower(input) != tolower(yes) && tolower(input) != tolower(no))
else if (tolower(input) != yes && tolower(input) != no) // <--- Changed the condition.
cout << "\n ERROR-2A: Invalid input. Must be a '" << static_cast<char>(toupper(yes)) << "' or '" << static_cast<char>(toupper(no)) << "' character.\n";
else
break;
} while (true);
return input;
}
int inputInteger(string prompt, int startRange, int endRange)
{
int input;
do
{
cout << prompt;
if (!(cin >> input))
{
cout << "\n ERROR-3A: Invalid input. Must be an integer type.\n";
cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); // <--- Requires header file <limits>.
}
//else if (!(input >= min(startRange, endRange) && input <= max(startRange, endRange)))
else if (!(input >= startRange && input <= endRange)) // <--- Changed the condition.
cout << "\n ERROR-3A: Invalid input. Must be from " << startRange << "..." << endRange << ".\n";
else
break;
} while (true);
return input;
}
string toUpper(string str)
{
for (size_t i = 0; i < str.length(); i++)
{
str.at(i) = toupper(str.at(i));
}
return str;
}
|
The header file "<cctype>" is for "std::tolower()" and "std::toupper" along with some other useful functions.
DO NOT count on your compiler and header files to include this at some point.
"<iomanip>" may be ahead of where you are at. If so the lines at the end of "main" that use "std::setw()" can be done by adding spaces to the "string".
At least give it a run and see what is displayed on the screen. You can change any part that you want.
In "main" your lines:
1 2
|
cout << "\n Enter a movie title: ";
cin >> movieTitle);
|
And the next one for "mpaaRating" are a problem. This is formatted input which means it will stop at a space or new line (\n) whichever comes first.
What you would end up with, with a movie title like "Gone With the Wind". All would be placed in the input buffer and
cin >> movieTitle;
would get "Gone" and the space, which it discards, leaving the rest in the input buffer. The the line
cin >> mpaaRating;
would get "With" and the space leaving "the Wind" in the input buffer.
Your next "cin >>" is to a numeric variable an it will not wait for the user to input a number it will try to put the "t" into the numeric variable causing "cin" to fail and entering the "if" part in the function "inputInteger" to deal with the error. When it is finished you have reset the state bits on "cin" and cleared the input buffer, so everything is fine after that.
I would say the better choice to clear the input buffer is:
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); // <--- Requires header file <limits>.
The do/while loop in "main" works, but not well. If ten people enter a rating for a movie how do you know how many times the loop has run. And if those ten people happen to enter the same rating how would you know how many people chose that rating?
As
MikeyBoy pointed the
addRating() function and the variables it uses do not have much use right now. Maybe later it will.
You said:
I’ve actually included those functions toward the end. moving them above main |
Moving them was not necessary All you needed was to put the prototypes above "main":
1 2 3
|
char inputChar(string prompt, char yes, char no);
int inputInteger(string prompt, int startRange, int endRange);
string toUpper(string str);
|
Usually I just copy the function definition and put that above "main" and add the semi-colon.
For the miner things: look closely at the above and see where I have added some blank lines. To the compiler this means nothing. To the reader it improves readability greatly.
After that there are some comments on things I have changed. The bold type should help find them.
Hope that helps,
Andy