Sarven Sheperd CPP help[SOLVED]

My character just stands there and won’t attack. I don’t understand why. Looking at different levels. Looking at the forum. My code seems right. Yet, my character just stands there! Little help why would be appreciated.

// Use while loops to pick out the ogre
int main() {
    
    while(true) {
        auto enemies = hero.findEnemies();
        int enemyIndex = 0;
    
        // Wrap this logic in a while loop to attack all enemies.
        // Find the array's length with:  enemies.size()
    while (enemyIndex > enemies.size()) {
        auto enemy = enemies[enemyIndex];
        // "!=" means "not equal to."
        if (enemy.type != "sand-yak") {
            // While the enemy's health is greater than 0, attack it!
            while (enemy.health > 0) {
                hero.attack(enemy); 
                } enemyIndex += 1;
        } 
        
    }
        // Between waves, move back to the center.
        
    }
    
    return 0;
}

In every iteration of your main while-true loop, you first declare enemyIndex to be 0 before looping across all enemies. However, your inner loop uses the condition enemyIndex > enemies.size()! This will never be true, as enemies.size() is always 0 or more, and so your attack logic is skipped. Repeat forever.

If your code does not act as expected it is often helpful to manually work out the steps, either mentally or with a piece of paper. Usually you’ll come across the error pretty quickly!

That is helpful but when I tried having it update the enemy index I would get ab error. I must be putting it in wrong or in the wrong place… Hmhm.

Nevermind. I figured it out… My enemyIndex += 1; was in wrong location

Please add a solved sign right next to the topic so people know that this problem is solved. Do it like this: [Solved] Sarven Shepherd CPP Help. Thank you!

This topic was automatically closed 12 hours after the last reply. New replies are no longer allowed.