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 user-defined 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 2.cpp
#include
#include
#include
#include
#include
#include
#include
using namespace std;
class Bag
{
private:
vector m_vec;
int m_amt;
int m_max;
public:
Bag(int amt, int max) : m_amt(amt), m_max(max)
{
}
Bag intersection(Bag userBag)
{
int count =0;
Bag interBag(m_amt, m_max);
for (int i =0; i < m_amt; i++)
{
for (int j =0; j < userBag.m_amt; j++)
{
if (m_vec[i]== userBag.m_vec[j])
{
interBag.addToVector(m_vec[i]);
userBag.m_vec.erase(userBag.m_vec.begin()+ j);
count++;
}
}
m_amt = count;
}
return interBag;
}
void addToVector(int userGuess)
{
m_vec.push_back(userGuess);
}
vector getVector()
{
return m_vec;
}
int getAmt()
{
return m_amt;
}
int getMax()
{
return m_max;
}
void printVector()
{
for (int i =0; i < m_amt; i++)
{
cout << m_vec[i]<<"";
}
}
};
///////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////
int main()
{
bool keepPlaying = true;
bool gameOver = false;
int amount;
int maximum;
int sameAmt;
srand(time(NULL));
while (keepPlaying == true)
{
cout << "Enter an amount of numbers to guess: ";
cin >> amount;
cout << endl << "Enter a maximum value to guess: ";
cin >> maximum;
Bag randBag(amount, maximum);
int count =0;
while (count < amount)
{
int random =1+(rand()% maximum);
randBag.addToVector(random);
count++;
}
randBag.printVector();
while (gameOver == false)
{
Bag guessBag(amount, maximum);
cout << endl << "Enter your guesses for the "<< amount <<" integers in the range from 1 to "
<< maximum <<" that have been selected." << endl;
int guessCount =0;
int guess;
while (guessCount < amount)
{
cin >> guess;
guessBag.addToVector(guess);
guessCount++;
}
guessBag.printVector();
Bag sameNumBag(amount, maximum);
sameNumBag = randBag.intersection(guessBag);
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?";
getline(cin, ans);
if (ans =="n")
{
keepPlaying = false;
}
}
}
}
return 0;
}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Finance Questions!