运行策划程序时出现Cygwin错误

Cygwin Error When Running Mastermind Program

本文关键字:Cygwin 错误 程序 运行      更新时间:2023-10-16

我正试图为我的编程类编写一个程序,该程序成功运行一个以数字1-6作为输入而不是颜色的游戏。当我尝试测试程序时,我得到消息

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

注释掉部分代码似乎没有多大帮助。我对这一切都是新手,所以任何经验教训都很感激。

任何帮助/建议都是非常感谢的!谢谢你阅读我的问题!
/** 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;
}

当您默认初始化它们时,假设所有向量都有四个元素。vector的默认初始化产生零元素的vector,因此当您访问vector的第一个到第四个元素时,就超出了vector的边界。

这是我正在谈论的一个简短的例子:

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

您有三个选项来修复此问题。也可以预先定义vector的大小:

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

或者您可以在填充时将其更改为适当的大小:

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

或者你可以在填充数组时动态调整数组的大小:

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

使用所有的语法,一旦您填充了向量,您就可以使用operator[]以您期望的方式访问元素。只是要确保不要超过向量的边界!您可以像这样调用size来检查向量有多大:myvector.size();