运行策划程序时出现Cygwin错误
Cygwin Error When Running Mastermind Program
我正试图为我的编程类编写一个程序,该程序成功运行一个以数字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();
相关文章:
- Windows/Cygwin - 不能使用 pybind11 - 犯错误
- 在cygwin中运行c++脚本时发生错误
- 在cygwin中测试新的boost安装时出现cpp错误
- NetBean 和 Cygwin 的 C++ 编译错误?
- 使用Cygwin C++时出现未定义的引用错误,为什么我的文件没有链接?
- FingerJetFXOSE Cygwin 编译错误
- 使用Cygwin win32_api.hpp编译Boost Log中的错误
- cygwin Windows 10错误“ sigfillset在范围”中没有声明
- 在Cygwin上更新为v5.4.0.CMATH会产生错误
- 在Cygwin上构建Nodejs时出现构建错误
- g++中的多重声明错误(cygwin)
- 在Windows 7上使用Cygwin为GCC构建提升 - 引导错误
- x86_64-pc-cygwin-gcc 链接器错误
- Cygwin gcc c++ c99 库错误 stod 无法识别使用的 strtod
- 在cygwin中使用clang的神秘链接器错误"undefined reference to `__gxx_personality_v0'"
- 模板中的C++Cygwin数字常量生成错误
- 在Cygwin中编译C++代码时出现权限错误
- regex.h中的编译错误cygwin没有这样的文件或目录
- cygwin/ Android -ndk中的Android - c++编译错误
- 运行策划程序时出现Cygwin错误