Question: I am trying to code an integer guessing game in which a user - defined random set of integers are generated. Afterwards, the user attempts
I am trying to code an integer guessing game in which a userdefined random set of integers are generated. Afterwards, the user attempts to guess those numbers. Based on how many they get correct, the program will let them know. To perform this action, I am trying to create a method called "intersection that detects the number of numbers in "guessBag" that the user correctly identified out of "randBag", and puts these correct numbers in "sameNumBag". I am not sure why, but I am currently unable to get the numbers in "sameNumBag". Therefore, I think something is wrong with my "intersection method? Can you please help me My current code is below. Thanks.
PRACTICE cpp
#include
#include
#include
#include
#include
#include
#include
using namespace std;
class Bag
private:
vector mvec;
int mamt;
int mmax;
public:
Bagint amt, int max : mamtamt mmaxmax
Bag intersectionBag userBag
int count ;
Bag interBagmamt, mmax;
for int i ; i mamt; i
for int j ; j userBag.mamt; j
if mveci userBag.mvecj
interBag.addToVectormveci;
userBag.mvec.eraseuserBagmvec.begin j;
count;
mamt count;
return interBag;
void addToVectorint userGuess
mvec.pushbackuserGuess;
vector getVector
return mvec;
int getAmt
return mamt;
int getMax
return mmax;
void printVector
for int i ; i mamt; i
cout mveci;
;
int main
bool keepPlaying true;
bool gameOver false;
int amount;
int maximum;
int sameAmt;
srandtimeNULL;
while keepPlaying true
cout "Enter an amount of numbers to guess: ;
cin amount;
cout endl "Enter a maximum value to guess: ;
cin maximum;
Bag randBagamount maximum;
int count ;
while count amount
int random rand maximum;
randBag.addToVectorrandom;
count;
randBag.printVector;
while gameOver false
Bag guessBagamount maximum;
cout endl "Enter your guesses for the amount integers in the range from to
maximum that have been selected." endl;
int guessCount ;
int guess;
while guessCount amount
cin guess;
guessBag.addToVectorguess;
guessCount;
guessBag.printVector;
Bag sameNumBagamount maximum;
sameNumBag randBag.intersectionguessBag;
sameAmt sameNumBag.getAmt;
if sameAmt amount
cout endl sameAmt of your guesses are correct. Guess again." endl;
else
gameOver true;
string ans;
cout "You are correct! Play again?";
getlinecin ans;
if ans n
keepPlaying false;
return ;
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
