简体   繁体   English

C ++程序停止执行响应

[英]C++ program stops responding on execution

Basic information regarding my computer: Windows 7 x64 OS 关于我的计算机的基本信息:Windows 7 x64 OS
8gb RAM 8GB RAM
Using MingW compiler 使用MingW编译器

So, everytime I run my program through the terminal after compiling it, it immediately stops responding. 因此,每次在编译后通过终端运行程序时,程序都会立即停止响应。 I can't seem to find the problem myself, so I'm asking for an extra set of eyes to point it out for me. 我似乎自己找不到问题,所以我要多花一些时间为我指出问题。 I'm practically a beginner at C++ at the moment, so bear with me. 目前,我实际上是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;
}

Also.... What is with this horrible code formatting.... I just can't get things to line up properly in the preview like it is when I'm writing it.... 还....这种可怕的代码格式是什么....我只是无法像在编写代码时一样使它们在预览中正确排列。

Dividing by zero? 除以零? secret.size() initially is 0. secret.size()最初为0。

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

You are dividing by zero. 您被零除。

In your line secret.push_back(pallete.at(rand()%secret.size())); 在您的行中secret.push_back(pallete.at(rand()%secret.size())); , secret.size() is zero. ,secret.size()为零。 According to this previous question's answer , Mod 0 is "Undefined, and will possibly throw a 'Divide by zero' exception. " 根据上一个问题的答案 ,Mod 0为“未定义,并且可能会引发'被零除'异常。”

I would probably change that line to secret.push_back(pallete.at(rand() * pallete.size()-1 )) , if you're trying to pick a number between 0 and your maximum number of potential colors to guess. 如果您尝试选择介于0和最大颜色数之间的数字,我可能会将该行更改为secret.push_back(pallete.at(rand() * pallete.size()-1 ))

I think you meant pallete.size in both locations (secret.size, guess.size). 我认为您的意思是在两个位置(secret.size,guess.size)中的pallet.size。 Then you have an infinite loop because blackP is never 4, and each run through the loop does the same thing since secretMem is never updated. 然后,您会遇到一个无限循环,因为blackP永远不会为4,并且由于secretMem从未更新,因此每次循环都执行相同的操作。 Maybe on Windows it was able to handle the overflow (somehow?) but got stuck in that loop. 也许在Windows上它能够处理溢出(以某种方式?),但是陷入了该循环。 Though in that case you'd get a lot of "you scored: 0 Black pegsand1 White pegs" lines. 尽管在这种情况下,您会得到很多“得分:0黑色钉和1白色钉”的信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM