Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8746585
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T12:13:04+00:00 2026-06-13T12:13:04+00:00

So, I’m creating a program to create a (sort of) simulation of a melee

  • 0

So, I’m creating a program to create a (sort of) simulation of a melee deathmatch video game (not actually making a video game at the moment, just making simple AIs with goals to kill each other). In order to do this, I am using a tile-based, turn-based system.

Now the introduction is out of the way, here is the specific problem: in one of the arrays I am using, the last value is stored incorrectly in RAM, no matter how many variables in the array. Here is the relevant code:

(I will post all the code I have at the bottom of this, but the problem is in here)

#include "stdafx.h"
#include <iostream>


int _tmain(int argc, _TCHAR* argv[])
{
    using namespace std;


    int npcTileAttacker[] = { 0, 0, 0, 0, 0};

    int s = 0;
    while (s < 6)
    {
        cout << "The value that is being selected from the array is " << s << endl;
        cout << npcTileAttacker[s] << endl;
        s++;
        cout << "The value of s has now been set to " << s << endl;
    }

This outputs:

The value that is being selected from the array is 0
0
The value of s has now been set to 1
The value that is being selected from the array is 1
0
The value of s has now been set to 2
The value that is being selected from the array is 2
0
The value of s has now been set to 3
The value that is being selected from the array is 3
0
The value of s has now been set to 4
The value that is being selected from the array is 4
0
The value of s has now been set to 5
The value that is being selected from the array is 5
-858993640
The value of s has now been set to 6

Obviously, this last value from the array is incorrect. What I want to know is why this would be happening.
In addition to this, when the program ends, I get an error message:
“Run-Time Check Failure #2 – Stack around the variable ‘npcTileAttacker’ was corrupted.”

I have tried placing the output values of s and the array piece of code around other arrays in the program, resulting in the same problem occuring.

Here is my full code, if required:

#include "stdafx.h"
#include <iostream>


int _tmain(int argc, _TCHAR* argv[])
{
    using namespace std;

int numberOfNPCs = 5;

//Remember whose alive (so we can skip the dead's turns)
int npcAlive[5] = { 1, 1, 1, 1, 1 };


/*This determines what action is going to be carried out on this square. For the moment:
if npcTileActivity[n] = 1;
the goals is death

WARNING! THIS WILL RESULT IN BUGS!!! I need to figure out a way that allows multiple activities on a tile 
(maybe apply actions onto NPCs directly, rather than onto their tiles)
*/
int npcTileActivity[] = { 0, 0, 0, 0, 0};



//This tells you who is doing the action on this tile
int npcTileAttacker[5] = { 1, 2, 3, 4, 0 };

    int s = 0;
while (s < 6)
{
    cout << "The value that is being selected from the array is " << s << endl;
    cout << npcTileAttacker[s] << endl;
    s++;
    cout << "The value of s has now been set to " << s << endl;
}
//This determines whether or not the NPC will fight back. Currently pointless, as this will just kill them.
int npcPacifism[5] = { 0 };



//This is their HP
int npcDefense[5] = {5, 5, 5, 5, 5};



//This is the default damage (presumably this is done with knives)
int npcOffense[5] = {1, 1, 1, 1, 1};



/*This determines what each NPC wants to do.
1   -   Kill Target
*/
int npcGoal[5] = {1, 1, 1, 1, 1};


//This is the NPC they are aiming at
int npcTarget[5] = {1, 2, 3, 4, 0};


/*The x coord for their target. In the future:
-I want this to be able to be changed during the sim
-This should be disabled until the NPC can find out where their target is
*/
int npcGoalLocationX[5] = {4, 1, 4, 3, 1};


/* The Y coord for their target
*/
int npcGoalLocationY[5] = {2, 3, 4, 2, 1};


/*Their x coord.
This will change, then the all npcGoalLocations need to be updated
*/
int npcLocationX[5] = {1, 4, 1, 4, 3};


/* Their y coord.
*/
int npcLocationY[5] = {1, 2, 3, 4, 2};

int m = 1;
while (m != 0)
{

    int i = 0;

    //Loop until every npc has had a turn
    while (i < (numberOfNPCs + 1))
    {
        /*npcGoalLocationY[i] = npcLocationY[npcTarget[i]];
        npcGoalLocationY[i] = npcLocationY[npcTarget[i]];*/
        if (npcAlive[i] = 1)
        {
            /* Tile activities:
            1 - Kill occupant
            */ 
            if (npcTileActivity[i] = 1)
            {
                cout << "This shouldn't be the first thing." << endl;

                //This gets the attack and defense values for the appropriate acting NPC
                int j = 0;
                while (j < (numberOfNPCs + 1))
                {

                    if (npcTileAttacker[i] = j)
                    {
                        //Defender's HP - Attacker's damage
                        int rollAttack1 = npcDefense[i] - npcOffense[j];
                        if (rollAttack1 > 0)
                            {
                            npcDefense[i] = rollAttack1;
                            cout << "NPC " << j << " attacks NPC " << i << endl;
                            if (npcPacifism[i] = 0)
                            {
                                //Defender retaliates
                                int rollAttack2 = npcDefense[j] - npcOffense[i];
                                if (rollAttack2 > 0)
                                {
                                    npcDefense[j] = rollAttack2;
                                    cout << "NPC " << i << " retaliates" << endl;
                                }else
                                {
                                    npcAlive[j] = 0;
                                    cout << "NPC " << j << " dies" << endl;
                                }
                            }
                        }else
                        {
                            npcAlive[i] = 0;
                            cout << "NPC " << i << " dies" << endl;
                        }
                    }
                    j++;
                }
            }else
            {
                cout << "This should be the first." << endl;
                if (npcGoal[i] != 0)
                {
                    if (npcGoalLocationX[i] = npcLocationX[i])
                    {
                        if (npcGoalLocationY[i] = npcLocationY[i])
                        {
                            //The Tile Activity of the current NPC's target is set to whatever the current NPC's goal is
                            npcTileActivity[npcTarget[i]] = npcGoal[i];
                        }
                    }
                    if (npcGoalLocationX[i] > npcLocationX[i])
                    {
                        npcLocationX[i]++;
                    }
                    if (npcGoalLocationX[i] < npcLocationX[i])
                    {
                        npcLocationX[i]--;
                    }
                    if (npcGoalLocationY[i] > npcLocationY[i])
                    {
                        npcLocationY[i]++;
                    }
                    if (npcGoalLocationY[i] < npcLocationY[i])
                    {
                        npcLocationY[i]--;
                    }
                }
            }
        }
        i++;
    }
    cin >> m;
}
return 0;
}

Also, I get a problem (around the lines which cout “This should be first” and “This shouldn’t be the first thing”): The one which shouldn’t be first is first and the one which should be first never even executes. This is probably related to the array error, however.

Thanks for your assistance.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T12:13:05+00:00Added an answer on June 13, 2026 at 12:13 pm

    Your condition is off by one:

    while (s < 6)  
    

    should be

    while (s < 5)
    

    The array { 0, 0, 0, 0, 0} has five elements, so valid indexes are 0,1,2,3,4.

    Your condition stops when s < 6 is false, so it’s still true for s == 5.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
Basically, what I'm trying to create is a page of div tags, each has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I need a function that will clean a strings' special characters. I do NOT
I'm trying to create an if statement in PHP that prevents a single post
I'm making a simple page using Google Maps API 3. My first. One marker
Is it possible to replace javascript w/ HTML if JavaScript is not enabled on
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.