Question

In: Computer Science

I am taking a Data Structures and Program Design class. BUT, I am having trouble getting...

I am taking a Data Structures and Program Design class. BUT, I am having trouble getting access to the vectors so that I can compare the guesses with the random numbers. I’ve been working on this for several days and don't feel any closer to a solution and could really use some help as I really don't know how to get this to work. Thank you!

The assignment is:

Design an ADT for a one-person guessing game that chooses 4 random integers in the range from 1 to 9 and asks the user to guess them. The same integer might be chosen more than once. For example, the game might choose the following four integers that range from 1 to 9: 4, 6, 1, 6. The following interaction could occur between the user and the game:

Enter your guesses for the 4 integers in the range from 1 to 9 that have been selected:

User Input: 1 2 3 4

2 of your guesses are correct. Guess again.

User Input: 2 4 6 8

2 of your guesses are correct. Guess again.

User Input: 1 4 6 6

You are correct! Play again? No

Good-bye!

Submit header file guess.h, implementation file guess.cpp, main function file main.cpp You need to include a makefile, and Readme file (see Programming Guidelines).

My code is:

---------------------------------main.cpp----------------------------------------

#include <iostream>     // needed for using cin/cou statements

#include <string>       // needed for using string type

#include <fstream>      // needed for using files

#include <ctime>    // Enables use of time() function

#include "guess.h"

using namespace std;

int main() {

    bool play = true;

    string answer;

    int randomNumsToGuess[3];

    int playerGuesses[4];

    srand(time(0));

    int matches = 0;

    bool match = false;

    Guess g1;

do {

    cout << "Begin" << endl; //just for following along

    do {

        cout << "Enter your first guess" << endl;

        cin >> playerGuesses[0];

        cin.ignore();

        cout << "Enter your second guess" << endl;

        cin >> playerGuesses[1];

        cin.ignore();

        cout << "Enter your third guess" << endl;

        cin >> playerGuesses[2];

        cin.ignore();

        cout << "Enter your fourth guess" << endl;

        cin >> playerGuesses[3];

        cin.ignore();

        Guess (playerGuesses[0], playerGuesses[1], playerGuesses[2], playerGuesses[3]);

        cout << "Sitting just before loop" << endl;

        for (int i = 0; i < 4; i++) {

            cout << "Entered first loop" << endl;

            for (int j = 0; j < 4; j++) {

                cout << "Entered second loop" << endl;

                //***** HERE IS WHERE I ERROR OUR ******

                if (g1.getVecGuesses(i) == g1.getVecRandomCopy(i)) {

                    matches = matches + 1;

                    //cout << "Match at playerGuess " << playerGuesses[i] << "and randomNum" << vecRandomCopy[j] << endl; //used during testing

                    g1.setVecGuesses(i);

                    g1.setVecRandomCopy(j);

                    //vecGuesses[i] = -1;

                    //vecRandomCopy[j] = -2;

                    //gameOver = true;

                    cout << "Matches: " << matches << endl;

                }

                cout << "Number of correct matches: " << matches << endl;

            }

        }

        if (matches == 3) {

            cout << "Congratulations! You guessed all three random numbers!" << endl;

        } else {

            cout << "Try again" << endl;

        }

    } while (matches != 3);

    matches = 0;

    cout <<"Would you like to play again?" << endl;

    cout << "Enter Y for yes or N for no." << endl;

    cin >> answer;

    cin.ignore();

    if (answer == "Y") {

        play = true;

    }

    else {

        play = false;

        cout << "Thanks for playing!" << endl;

    }

} while (play == true);

    return 0;

};

-------------------------------Guess.h-------------------------------------------

//

// Created by craig on 9/4/2019.

//

#ifndef DSPD_HW1_GUESS_H

#define DSPD_HW1_GUESS_H

#include <vector>

using namespace std;

class Guess{

private:

    vector <int> vecRandom;

    vector <int> vecRandomCopy;

    vector <int> vecGuesses;

    int randomNum;

    int matches = 0;

public:

    Guess();

    Guess(int a, int b, int c, int d);

    void Matches (vector<int> vecRandomCopy, vector<int> vecGuesses);

    void showVecGuesses(vector<int>);

    //might need to Guess::vecGuesses[x] = -1;

    void setVecGuesses (int x) {

        vecGuesses[x] = -1;}

    //void setVecGuesses (vector<int> _vecGuesses(int x)) {vecGuesses[x] = _vecGuesses[x];}

    void setVecRandomCopy (int y) {

        vecRandomCopy[y] = -2;}

    /*void displayVecContents (){

        for (unsigned int i=0; i<vecGuesses.size(); i++ )

            cout << "Guesses " << vecGuesses[i] << endl;

    }

*/

    int getVecGuesses (int x) {

        cout << "TEST " << vecGuesses[x];

        return vecGuesses[x];}

    int getVecRandomCopy (int y) {

        return vecRandomCopy[y];}

    int Matches (vector<Guess> & ) {

        cout << "Testing Matches" << endl;

    }

};

#endif //DSPD_HW1_GUESS_H

--------------------------guess.cpp --------------------------------------

//

// Created by craig on 9/4/2019.

//

#include <iostream>     // needed for using cin/cou statements

#include <string>       // needed for using string type

#include <fstream>      // needed for using files

#include <ctime>    // Enables use of time() function

#include <vector>

#include "guess.h"

using namespace std;

Guess::Guess() {//default constructor, fills with random numbers

        for (int i = 0; i < 4; i++) {

            randomNum = (rand() % 9)+1;

            vecRandom.push_back(randomNum);

            cout << "Random number " << i + 1 << ": " << vecRandom[i] << endl;

        }

        for (int i=0; i < 4; i++) { //creates a copy of the random numbers so I can manipulate it without

                                    //losing the original random numbers

            vecRandomCopy = vecRandom;

            cout << "Random number copy " << i + 1 << ": " << vecRandomCopy[i] << endl;

        }

    }

Guess::Guess (int a, int b, int c, int d){ //constructor for user guesses

    vecGuesses.push_back(a);

    vecGuesses.push_back(b);

    vecGuesses.push_back(c);

    vecGuesses.push_back(d);

    cout << "You Guessed: ";

    for (int i = 0; i < 4; i++) {

        cout << " " << vecGuesses[i];

    }

    cout << endl;

}

void showVecGuesses(vector<int> _vecGuesses){

    for (int i=0; i<4; i++){

        cout << "TESTING SHOW VEC GUESSES" << endl;

        cout << _vecGuesses[i] << endl;

    }

}

void Matches (vector<int> _vecRandomCopy, vector<int> _vecGuesses){

    cout << "TESTING: do we get into matches?" << endl;

}

Solutions

Expert Solution

cpp code:

main.cpp

#include <iostream> // needed for using cin/cou statements

#include <string> // needed for using string type

#include <fstream> // needed for using files

#include <ctime> // Enables use of time() function

#include <stdlib.h>

#include "guess.h"

using namespace std;

//main
int main() {

bool play = true;

string answer;

int playerGuesses[4];

srand(time(0));

int matches = 0;


//loop starts
do {
Guess g1; //object for guess class (invoke the constructor & set the guess

do {

//get numbers from user
cout << "Enter your first guess" << endl;

cin >> playerGuesses[0];

// cin.ignore();

cout << "Enter your second guess" << endl;

cin >> playerGuesses[1];

// cin.ignore();

cout << "Enter your third guess" << endl;

cin >> playerGuesses[2];

//cin.ignore();

cout << "Enter your fourth guess" << endl;

cin >> playerGuesses[3];

// cin.ignore();

/*rather than passing the user guess as constructor with parameter, set the values with function.
when you set two constructor the previous value will be deleted */

//set the user guess with function
g1.setUserGuess(playerGuesses[0], playerGuesses[1], playerGuesses[2], playerGuesses[3]);

  
//check the match
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {

//if guess equals
if (g1.getVecGuesses(i) == g1.getVecRandomCopy(j)) {
matches = matches + 1; //increase the match
g1.setVecRandomCopy(j); //set the random copy to -1
}
}
}
//print the matches
cout << "\n\nNumber of correct matches: " << matches << endl;

//if matches >3, print msg
if (matches > 3) {

cout << "\nCongratulations! You guessed all three random numbers!" << endl;

} else {
//if not, reset the random copy and matches to 0
cout << "\nTry again" << endl;
g1.resetCopy();
matches = 0;

}

} while (matches <4); //if matches not found repeat the loop

//ask user to play again

cout <<"\nWould you like to play again?" << endl;

cout << "Enter Y for yes or N for no." << endl;

cin >> answer;

cin.ignore();

if (answer == "Y") {

play = true;

}

else {

play = false;

cout << "\n\nThanks for playing!" << endl;

}

} while (play == true);

return 0;

};

Guess.h

#ifndef GUESS_H_INCLUDED
#define GUESS_H_INCLUDED

#include <vector>

using namespace std;

class Guess{

private:

vector <int> vecRandom;

vector <int> vecRandomCopy;

vector <int> vecGuesses;

int randomNum;

int matches = 0;

public:

Guess();

void setUserGuess(int a, int b, int c, int d);

void Matches (vector<int> vecRandomCopy, vector<int> vecGuesses);

void showVecGuesses(vector<int>);

//might need to Guess::vecGuesses[x] = -1;

void setVecGuesses (int x) {

vecGuesses[x] = -1;}

//void setVecGuesses (vector<int> _vecGuesses(int x)) {vecGuesses[x] = _vecGuesses[x];}

void setVecRandomCopy (int y) {

vecRandomCopy[y] = -2;}

  

int getVecGuesses (int x) {

// cout << "TEST " << vecGuesses[x];

return vecGuesses[x];
}

int getVecRandomCopy (int y) {

return vecRandomCopy[y];
}

void Matches (vector<Guess> & ) {

cout << "Testing Matches" << endl;

}
void resetCopy();


};

#endif // GUESS_H_INCLUDED

Guess.cpp

#include <iostream> // needed for using cin/cou statements

#include <string> // needed for using string type

#include <fstream> // needed for using files

#include <ctime> // Enables use of time() function

#include <vector>
#include <stdlib.h>

#include "guess.h"

using namespace std;

Guess::Guess()
{//default constructor, fills with random numbers

for (int i = 0; i < 4; i++) {

randomNum = (rand() % 9)+1;

vecRandom.push_back(randomNum);

cout << "Random number " << i + 1 << ": " << vecRandom[i] << endl;

}

for (int i=0; i < 4; i++) { //creates a copy of the random numbers so I can manipulate it without

//losing the original random numbers

vecRandomCopy = vecRandom;

cout << "Random number copy " << i + 1 << ": " << vecRandomCopy[i] << endl;

}

}

//reset the random copy with original guess
void Guess::resetCopy()
{
vecRandomCopy = vecRandom;

}
//function for set the user guesses
void Guess::setUserGuess(int a, int b, int c, int d){

vecGuesses.clear(); //clears the previous values

vecGuesses.push_back(a);

vecGuesses.push_back(b);

vecGuesses.push_back(c);

vecGuesses.push_back(d);

cout << "You Guessed: ";

for (int i = 0; i < 4; i++) {

cout << " " << vecGuesses[i];

}

cout << endl;

}

//no need this function (upto you to delete)
void showVecGuesses(vector<int> _vecGuesses){

for (int i=0; i<4; i++){

cout << "TESTING SHOW VEC GUESSES" << endl;

cout << _vecGuesses[i] << endl;

}

}

//no need this function (upto you to delete)
void Matches (vector<int> _vecRandomCopy, vector<int> _vecGuesses){

cout << "TESTING: do we get into matches?" << endl;

}

output:

I have fixed your code and mentioned the error in the code. some of your functions are ambiguous , can delete it if it has no further use.

if you have any issues please do comments . if you found this solutions useful, please give me thumbs up.


Related Solutions

I am having a trouble with a python program. I am to create a program that...
I am having a trouble with a python program. I am to create a program that calculates the estimated hours and mintutes. Here is my code. #!/usr/bin/env python3 #Arrival Date/Time Estimator # # from datetime import datetime import locale mph = 0 miles = 0 def get_departure_time():     while True:         date_str = input("Estimated time of departure (HH:MM AM/PM): ")         try:             depart_time = datetime.strptime(date_str, "%H:%M %p")         except ValueError:             print("Invalid date format. Try again.")             continue        ...
I am having trouble with my assignment and getting compile errors on the following code. The...
I am having trouble with my assignment and getting compile errors on the following code. The instructions are in the initial comments. /* Chapter 5, Exercise 2 -Write a class "Plumbers" that handles emergency plumbing calls. -The company handles natural floods and burst pipes. -If the customer selects a flood, the program must prompt the user to determine the amount of damage for pricing. -Flood charging is based on the numbers of damaged rooms. 1 room costs $300.00, 2 rooms...
Hello! I am having trouble starting this program in Java. the objective is as follows: "...
Hello! I am having trouble starting this program in Java. the objective is as follows: " I will include a text file with this assignment. It is a text version of this assignment. Write a program that will read the file line by line, and break each line into an array of words using the tokenize method in the String class. Count how many words are in the file and print out that number. " my question is, how do...
Hello, I am having trouble getting started on my project and building these functions. How do...
Hello, I am having trouble getting started on my project and building these functions. How do I build a function that continuously adds new "slices" to the list if they are below/above the size limit? I didn't copy the entire problem, but just for reference, when the code is run it will take user input for size limit (L), time cost for a random slice(R), and time cost for an accurate slice(A). Question: In real life, a steak is a...
I was able to calculate (a) but I am having trouble with the calculations of (b)....
I was able to calculate (a) but I am having trouble with the calculations of (b). Thanks! The following are New York closing rates for A$/US$ and $/£:                                     A$/$ = 1.5150;               $/£ = $1.2950             (a) Calculate the cross rate for £ in terms of A$ (A$/£).             (b) If £ is trading at A$1.95/£ in London (cross market) on the same day, is there an arbitrage opportunity?  If so, show how arbitrageurs with £ could profit from this opportunity and calculate the arbitrage...
For some reason I am having a hard time getting this program to compile properly. Could...
For some reason I am having a hard time getting this program to compile properly. Could you help me debug it? Write the prototypes and functions to overload the given operators in the code //main.cpp //This program shows how to use the class rectangleType. #include <iostream> #include "rectangleType.h" using namespace std; int main() {     rectangleType rectangle1(23, 45);                     //Line 1     rectangleType rectangle2(12, 10);                     //Line 2     rectangleType rectangle3;                             //Line 3     rectangleType rectangle4;                             //Line 4     cout << "Line...
I am working through this solution in rstudio and am having trouble fitting this table into...
I am working through this solution in rstudio and am having trouble fitting this table into a linear regression analysis. an answer with corrosponding r code used would be greatly appreciated A study was conducted to determine whether the final grade of a student in an introductory psychology course is linearly related to his or her performance on the verbal ability test administered before college entrance. The verbal scores and final grades for all 1010 students in the class are...
I am having the most trouble with 1d: 1. a. Prove that if f : A...
I am having the most trouble with 1d: 1. a. Prove that if f : A → B, g : B → C, and g ◦f : A → C is a 1-to-1 surjection, then f is 1-to-1 and g is a surjection. Proof. b. Prove that if f : A → B, g : B → C, g ◦ f : A → C is a 1-to-1 surjection, and g is 1-to-1, then f is a surjection. Proof. c....
I am having trouble with a C++ code that I'm working on. It is a spell...
I am having trouble with a C++ code that I'm working on. It is a spell checker program. It needs to compare two arrays, a dictionary, and an array with misspelled strings that are compared to the strings in the dictionary. the strings that are in the second array that is not in the Dictionary are assumed to be misspelled. All of the strings in the dictionary are lowercase without any extra characters so the strings that are passed into...
I am working on these study questions and am having trouble understanding how it all works...
I am working on these study questions and am having trouble understanding how it all works together. Any help would be greatly appreciated!! An all equity firm is expected to generate perpetual EBIT of $50 million per year forever. The corporate tax rate is 0% in a fantasy no tax world. The firm has an unlevered (asset or EV) Beta of 1.0. The risk-free rate is 5% and the market risk premium is 6%. The number of outstanding shares is...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT