Search code examples
c++vectorcygwin

Cygwin Error When Running Mastermind Program


I am trying to write a program for my programming class that successfully runs a game of mastermind with numbers 1-6 as inputs instead of colors. When I try to test the program as is I get the message

" 0 [main] Lab16 9828 cygwin_exception::open_stackdumpfile: Dumping stack trace to Lab16.exe.stackdump"

Commenting out sections of the code does not seem to help much. I am quite the noobie to all of this so any lessons learned are appreciated.

Any help/suggestions are greatly appreciated! Thank you for reading my question!

/** INCLUDE FILES ***************************************************/

#include <iostream> //  input output commands:  cout & cin
#include <iomanip>
#include <vector>
#include <cmath>
#include <cstdlib>
using namespace std;

/** FUNCTION PROTOTYPES**********************************************/
void GetPatterns(vector <int> &x); // Gets user pattern
void CreateSolution(vector <int> &y); // Creates the right pattern before user input
bool SolutionCalc(vector <int> x, vector <int> y); // Detects how many guesses are correct and or in the right place, returns bool value to main()
/** MAIN FUNCTION ***************************************************/
int main()
{
    /** VARIABLE DECLARATION ****************************************/
    bool solution;
    vector <int> UserPattern;
    vector <int> RealPattern;
    srand(time(0));
    /** FUNCTION CALLS***********************************************/
    CreateSolution(RealPattern);
    do
    {
    GetPatterns(UserPattern);
    solution = SolutionCalc(UserPattern,RealPattern);
    }while(solution == false);

    cout << "Correct!" << endl;
    cout << "You are a Mastermind!" << endl;



    return 0;
}

/** FUNCTIONS *******************************************************/

void GetPatterns(vector <int> &x)
{
    cout << "Welcome to Mastermind." << endl;
    cout << endl;
    cout << "Please enter your four numerical guesses(space separated, numbers 1-6): ";
    for (int i = 0; i < 4; i++) // 4 size vector array for user input
    {
        cin >> x[i];
    }
    cout << endl;
}

void CreateSolution(vector <int> &y)
{
    for(int e = 0; e < 4; e++) // 4 size vector array for solution
        {
            y[e] = rand()%6+1;
        }
    cout << endl;
}

bool SolutionCalc(vector <int> x, vector <int> y) // Z is the bool to check if the solution is solved or not
{
    int RightNum = 0, RightPlace = 0;
    bool IsSolution;
    for (int i = 0; i < 4; i++)
    {
        if (x[i] == y[i])
        {
            RightPlace++;
        }

        if ((x[i] != y[i]))
        {
            if(x[i] == y[0] || x[i] == y[1] || x[i] == y[2] || x[i] == y[3])
            {
                RightNum++;
            }
        }

    }
    if (RightNum < 4)
    {
        cout << "You have " << RightNum << " correct number(s) and " << RightPlace << " correct locations(s)." << endl;
        IsSolution = false;
    }
    else if (RightNum == 4)
    {
        IsSolution = true;
    }

    return IsSolution;
}

Solution

  • You're assuming that all your vectors have four elements, when you've default-initialized them. Default-initialization for vectors produces vectors with zero elements, so when you access the first through fourth elements of the vectors, you exceed the bounds of the vector.

    This is a short example of what I'm talking about:

    std::vector<int> myvector;
    myvector[1] = 3; // oh no!
    

    You have three options for fixing this. Either you can predefine the size of the vector:

    std::vector<int> myvector(4);
    myvector[1] = 3; // ok
    

    or you can change it to the appropriate size while you're populating it:

    std::vector<int> myvector; // elsewhere
    
    myvector.resize(4);
    myvector[1] = 3; // okay
    

    or you can dynamically adjust the size of the array when you're populating it:

    std::vector<int> myvector; // elsewhere
    
    for(size_t index = 0; index < 4; ++index){
        myvector.push_back(someNumber); // also okay
    }
    

    With all the syntaxes, once you've populated your vector, you can access elements the way you expect, with operator[]. Just make sure not to exceed the bounds of the vector! You can check how big a vector is with a call to size like so: myvector.size();