gpt4 book ai didi

C++ 程序在执行时停止响应

转载 作者:行者123 更新时间:2023-11-30 00:52:49 25 4
gpt4 key购买 nike

关于我的电脑的基本信息:Windows 7 x64 操作系统
8GB 内存
使用 MingW 编译器

因此,每次我在编译后通过终端运行我的程序时,它都会立即停止响应。我自己似乎找不到问题,所以我要求多一双眼睛为我指出。我目前几乎是 C++ 的初学者,所以请多多包涵。

    #include <iostream>
#include <cstdlib>
#include <string>
#include <vector>
#include <ctime>

using namespace std;

int main()
{
bool correct = false;
srand(time(NULL));
vector<string> guess;
vector<string> pallete;
vector<string> secret;
pallete.push_back("red");
pallete.push_back("green");
pallete.push_back("blue");
pallete.push_back("yellow");
for(int i = 0; i < 4 ; i++)
{
secret.push_back(pallete.at(rand()%secret.size()));
}
for(int j = 0; j < 4 ; j++)
{
guess.push_back(pallete.at(rand()%guess.size()));
}
vector<string> secretMem = secret;
cout << "the guess was:";
for(int x = 0; x<4; x++)
{
cout << guess[x] << endl;
}

while(correct == false)
{
int blackP = 0;
int whiteP = 0;
secret = secretMem;
for (int idxB = 0; idxB < secret.size(); idxB++)
{
if (guess[idxB] == secret[idxB])
{
secret[idxB] = "0";
guess[idxB] = "1";
blackP++;
}
}
for (int idxW = 0; idxW < secret.size(); idxW++)
for (int idyW = 0; idyW < guess.size(); idyW++)
{
if (secret[idxW] == guess[idyW])
{
secret[idxW] = "0";
guess[idxW] = "1";
whiteP++;
}
}
if (blackP == 4)
{
cout << "Congratulations you win." << endl << "The secret code was:"<< endl;
for(int y = 0; y<4; y++)
{
cout << secretMem[y] << endl;
}
correct = true;
}
else
cout << "you scored: " << blackP << " Black pegs" << "and" << whiteP << " White pegs" << endl;
}
return 0;
}

另外....这种可怕的代码格式是怎么回事....我无法像编写代码时那样在预览中正确排列....

最佳答案

除以零? secret.size() 初始值为 0。

secret.push_back(pallete.at(rand()%secret.size()));

关于C++ 程序在执行时停止响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18084382/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com