In: Computer Science
For this programming assignment, you will use your previous code that implemented a video game class and objects with constructors. Add error checking to all your constructors, except the default constructor which does not require it. Make sure that the high score and number of times played is zero or greater (no negative values permitted). Also modify your set methods to do the same error checking. Finally add error checking to any input requested from the user.
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
class VideoGame {
private:
string name;
int highScore;
int numOfPlays;
public:
VideoGame() {
name = "NA";
highScore = 0;
numOfPlays = 0;
}
VideoGame(string n, int score, int num) {
name = n;
highScore = score;
numOfPlays = num;
}
VideoGame(string n) {
name = n;
highScore = 0;
numOfPlays = 0;
}
VideoGame(const VideoGame &p2)
{
name = p2.name;
highScore = p2.highScore;
numOfPlays = p2.numOfPlays;
}
void setName(string n) {
name = n;
}
void setHighScore(int score) {
highScore = score;
}
void setNumOfPlays(int num) {
numOfPlays = num;
}
string getName() {
return name;
}
int getHighScore() {
return highScore;
}
int getNumOfPlays() {
return numOfPlays;
}
};
string inputName() {
string n;
cout << "Enter name of video game: ";
getline(cin >> ws, n);
return n;
}
int inputHighScore() {
int score;
cout << "Enter current high score of game: ";
cin >> score;
return score;
}
int inputNumOfPlays() {
int num;
cout << "Enter number of times game is played: ";
cin >> num;
return num;
}
void output(VideoGame game) {
cout << game.getName() << endl
<< "Played " << game.getNumOfPlays() << " times (HIGH SCORE " << game.getHighScore() << ")" << endl;
}
int main() {
VideoGame game1, game2, game3("VideoGame 3");
game1.setName(inputName());
game1.setHighScore(inputHighScore());
game1.setNumOfPlays(inputNumOfPlays());
cout << "\nGame1" << endl;
output(game1);
cout << endl;
game2.setName(inputName());
game2.setHighScore(inputHighScore());
game2.setNumOfPlays(inputNumOfPlays());
cout << "\nGame2" << endl;
output(game2);
cout << "\nGame3" << endl;
output(game3);
VideoGame game4 = game2;
cout << "\nGame4" << endl;
output(game4);
return 0;
}
Raw_code:
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
class VideoGame {
private:
string name;
int highScore;
int numOfPlays;
public:
VideoGame() {
name = "NA";
highScore = 0;
numOfPlays = 0;
}
VideoGame(string n, int score, int num) {
name = n;
if(score < 0){
cout<<"Invalid Score!. No Negative values
permitted."<<endl;
exit(0);
}
else
highScore = score;
if(num < 0){
cout<<"Invalid plays!. No Negative values
permitted."<<endl;
exit(0);
}
else
numOfPlays = num;
}
VideoGame(string n) {
name = n;
highScore = 0;
numOfPlays = 0;
}
VideoGame(const VideoGame &p2){
name = p2.name;
if(p2.highScore < 0){
cout<<"Invalid Score!. No Negative values
permitted."<<endl;
exit(0);
}
else
highScore = p2.highScore;
if(p2.numOfPlays < 0){
cout<<"Invalid plays!. No Negative values
permitted."<<endl;
exit(0);
}
else
numOfPlays = p2.numOfPlays;
}
void setName(string n) {
name = n;
}
void setHighScore(int score) {
if(score < 0){
cout<<"Invalid Score!. No Negative values
permitted."<<endl;
exit(0);
}
else
highScore = score;
}
void setNumOfPlays(int num) {
if(num < 0){
cout<<"Invalid plays!. No Negative values
permitted."<<endl;
exit(0);
}
else
numOfPlays = num;
}
string getName() {
return name;
}
int getHighScore() {
return highScore;
}
int getNumOfPlays() {
return numOfPlays;
}
};
string inputName() {
string n;
cout << "Enter name of video game: ";
getline(cin >> ws, n);
return n;
}
int inputHighScore() {
int score;
cout << "Enter current high score of game: ";
cin >> score;
while(score < 0){
cout<<"Invalid Score!. No Negative values
permitted."<<endl;
cout<<"Enter current high score of game: ";
cin>>score;
}
return score;
}
int inputNumOfPlays() {
int num;
cout << "Enter number of times game is played: ";
cin >> num;
while(num < 0){
cout<<"Invalid plays!. No Negative values
permitted."<<endl;
cout << "Enter number of times game is played: ";
cin>>num;
}
return num;
}
void output(VideoGame game) {
cout << game.getName() << endl
<< "Played " << game.getNumOfPlays() << " times
(HIGH SCORE " << game.getHighScore() << ")" <<
endl;
}
int main() {
VideoGame game1, game2, game3("VideoGame 3");
game1.setName(inputName());
game1.setHighScore(inputHighScore());
game1.setNumOfPlays(inputNumOfPlays());
cout << "\nGame1" << endl;
output(game1);
cout << endl;
game2.setName(inputName());
game2.setHighScore(inputHighScore());
game2.setNumOfPlays(inputNumOfPlays());
cout << "\nGame2" << endl;
output(game2);
cout << "\nGame3" << endl;
output(game3);
VideoGame game4 = game2;
cout << "\nGame4" << endl;
output(game4);
return 0;
}