1

Basic information regarding my computer: Windows 7 x64 OS
8gb RAM
Using MingW compiler

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.

    #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....

3 Answers3

5

Dividing by zero? secret.size() initially is 0.

secret.push_back(pallete.at(rand()%secret.size()));
hauron
  • 4,550
  • 5
  • 35
  • 52
1

You are dividing by zero.

In your line secret.push_back(pallete.at(rand()%secret.size())); , secret.size() is zero. According to this previous question's answer, Mod 0 is "Undefined, and will possibly throw a 'Divide by zero' exception. "

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.

Community
  • 1
  • 1
Ricky Mutschlechner
  • 4,291
  • 2
  • 31
  • 36
0

I think you meant pallete.size in both locations (secret.size, guess.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. Maybe on Windows it was able to handle the overflow (somehow?) but got stuck in that loop. Though in that case you'd get a lot of "you scored: 0 Black pegsand1 White pegs" lines.

seanmcl
  • 9,740
  • 3
  • 39
  • 45