-3

I'm making a game in C++ which I hope can successfully create a universe of its own procedurally, however, I've encountered an assertion error with the galaxy generation. I have a class for Stars with the following parameters for the constructor:

Stars(std::string newStarName = WordGenerator(), double starPicker = std::rand() % 100, int newStarPosX = std::rand() % 500, int newStarPosY = std::rand() % 500)

I also have a function which can create stars at random:

void GenerateStar(std::string newStarName, double starPicker, int newStarPosX, int newStarPosY) {
Stars newStar(newStarName,  starPicker,  newStarPosX,  newStarPosY);
//allStars.push_back(newStar);
allStarsNames.push_back(newStar.starName);}

And the Galaxy class constructor is as follows:

Galaxy() {
    int starCounter = 0;
    while (starCounter < 5) {
        GenerateStar(WordGenerator(), std::rand() % 100, std::rand() % 500, std::rand() % 500);
        starCounter++;
    }
    

        
    
}

When I try running the program after initializing a galaxy in main visual studio gives me an error screen. error box

I've tried changing the default arguments, changing the parameters of the GenerateStar function, and changed which arguments were passed into the function call in the Galaxy constructor but nothing seems to work.

The definition of the WordGenerator function:

std::string WordGenerator() {
std::vector<char> alphabet = { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z' };
std::string word;
int counter = 2 + std::rand() % 5;
while (counter > 0) {
   word = word + alphabet[std::rand() % alphabet.size()];
   counter--;
}
for (int i = 0; i < word.size(); i++) {
    if (CohrnceAnalyzer(word[i], word[i + 1]) == true) {
        word[i+1] = alphabet[std::rand() % alphabet.size()];
    }
}
return word;}

And for the Coherence Analyzer function:

bool CohrnceAnalyzer(char firstLetter, char secondLetter) {
std::vector<char> alphabet = { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'};
std::vector<char> firstLetters = { 'b', 'b', 'b', 'b', 'b', 'b', 'b', 'b', 'c', 'c', 'c', 'c', 'c', 'c', 'c', 'd','d', 'd', 'd', 'd', 'd', 'd', 'd', 'd', 'd', 'f', 'f' };
std::vector<char> secondLetters = { 'd', 'f', 'g', 'm', 'n', 'p', 'q', 'x', 'b', 'g', 'j', 'm', 'n', 'v', 'w', 'b', 'c', 'd', 'f', 'g', 'm', 'n', 'p', 'q', 'x', 'b', 'j'};
for (int i = 0; i < firstLetters.size(); i++) {
    if (firstLetter == firstLetters[i] && secondLetter == secondLetters[i]) {
        
        return true;
    }
    else {
        return false;
    }}}

1 Answers1

3

OK, here's the string subscripting error

for (int i = 0; i < word.size(); i++) {
    if (CohrnceAnalyzer(word[i], word[i + 1]) == true) {
        word[i+1] = alphabet[std::rand() % alphabet.size()];
    }
}

When i == word.size() - 1 then i + 1 == word.size() and so word[i+1] is a string subscripting error.

The bug would go away if you changed i < word.size(); to i + 1 < word.size(); which I think also makes sense with what you are trying to do in that function. But really only you can decide on that.

john
  • 85,011
  • 4
  • 57
  • 81