Question

In: Computer Science

For this programming assignment, you will use your previous code that implemented a video game class...

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;

}

Solutions

Expert Solution

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;
}


Related Solutions

c++ Programming For this assignment you will be building on your Fraction class. However, the changes...
c++ Programming For this assignment you will be building on your Fraction class. However, the changes will be significant, so I would recommend starting from scratch and using your previous version as a resource when appropriate. You'll continue working on your Fraction class for one more week, next week. For this week you are not required to provide documentation and not required to simplify Fractions. Please keep all of your code in one file for this week. We will separate...
JAVA PROGRAMMING. In this assignment, you are to create a class named Payroll. In the class,...
JAVA PROGRAMMING. In this assignment, you are to create a class named Payroll. In the class, you are to have the following data members: name: String (5 pts) id: String   (5 pts) hours: int   (5 pts) rate: double (5 pts) private members (5 pts) You are to create no-arg and parameterized constructors and the appropriate setters(accessors) and getters (mutators). (20 pts) The class definition should also handle the following exceptions: An employee name should not be empty, otherwise an exception...
You are to use your code from part A of this assignment and add to it....
You are to use your code from part A of this assignment and add to it. In this part of the project, you are to do the following: 1. In the count_voters function you are to add code to: • Count the number of votes for Trump • Count the number of votes for Biden • Count the number of females • Count the number of males • Count the number of Democrats • Count the number of Republicans •...
In this programming assignment, you will write C code that performs recursion. For the purpose of...
In this programming assignment, you will write C code that performs recursion. For the purpose of this assignment, you will keep all functions in a single source file main.c. Your main job is to write a recursive function that generates and prints all possible password combinations using characters in an array. In your main() function you will first parse the command line arguments. You can assume that the arguments will always be provided in the correct format. Remember that the...
Code in C Instructions For this programming assignment you are going to implement a simulation of...
Code in C Instructions For this programming assignment you are going to implement a simulation of Dijkstra’s solution to the Dining Philosophers problem using threads, locks, and condition variables. Dijkstra’s Solution Edsgar Dijkstra’s original solution to the Dining Philosophers problem used semaphores, but it can be adapted to use similar mechanisms: • Each philosopher is in one of three states: THINKING, HUNGRY, or EATING. • Every philosopher starts out in the THINKING state. • When a philosopher is ready to...
C++ In this assignment you will use your Card class to simulate a deck of cards....
C++ In this assignment you will use your Card class to simulate a deck of cards. You will create a Deck class as follows: The constructor will create a full 52-card deck of cards. 2, 3, 4, 5, 6, 7, 8, 9, 10, Jack, Queen, King, Ace for each suit: Clubs, Diamonds, Hearts, Spades Each card will be created on the heap and the deck will be stored using an STL vector of Card pointers. The destructor will free the...
Describe a game you could use in your early childhood class to facilitate the development of...
Describe a game you could use in your early childhood class to facilitate the development of articulation skills. Discuss in what ways you would use this game, and how it encourages development of articulation skills.
FOR C++ A friend wants you to start writing a video game. Write a class called...
FOR C++ A friend wants you to start writing a video game. Write a class called “Player” that includes the location of the player, her/his current score, and the ancestry of the player (e.g. “Elf”, “Goblin”, “Giant”, “Human”). A player will always start at location (0, 0) and have a score of 0. Write accessors/modifiers (or “setters/getters”) for each of those characteristics. Write an “appropriate” constructor for the class (based on what’s described above). Write methods moveNorth(), moveSouth(), moveEast() and...
Instructions Use your solution from Programming Assignment 5 (or use your instructor's solution) to create a...
Instructions Use your solution from Programming Assignment 5 (or use your instructor's solution) to create a modular Python application. Include a main function (and a top-level scope check),and at least one other non-trivial function (e.g., a function that deals the new card out of the deck). The main function should contain the loop which checks if the user wants to continue. Include a shebang, and ID header, descriptive comments, and use constants where appropriate. Sample Output (user input in red):...
Instructions Use your solution from Programming Assignment 5 (or use your instructor's solution) to create a...
Instructions Use your solution from Programming Assignment 5 (or use your instructor's solution) to create a modular Python application. Include a main function (and a top-level scope check),and at least one other non-trivial function (e.g., a function that deals the new card out of the deck). The main function should contain the loop which checks if the user wants to continue. Include a shebang, and ID header, descriptive comments, and use constants where appropriate. Sample Output (user input in red):...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT