I have NO idea what this error is

It states: Run-Time Check Failure#2 - Stack around the variable 'list' was corrupted. Can anyone help me figure this out? This program runs and it does what its supposed to do... I'm stumpt as to what I need to do.

#include <iostream>

using namespace std;

int removeAt(int [], int, int);

int main ()
{
int list[7] = {5, 3, 6, 7, 1, 4, 2};
int y = 0;
int searchItem = 0;
int elements = 7;
bool found = false;

cout << "Here is the unsorted list: " << endl;

for (y = 0; y < 7; y++)
{
cout << list[y] << " ";
}

cout << endl;

cout << endl;
cout << "What number do you want to erase from the list?" <<endl;
cin >> searchItem;

for(y = 0; y < elements; y++)
{
if (list[y] == searchItem)
found = true;
}

if (found == true)
{
elements = removeAt(list, elements, searchItem);
cout << "Here is the list minus your number: ";
for(y = 0; y < elements; y++)
{
cout << list[y] << " ";
}

cout << endl;
}
else
cout << "Number not found." << endl;
return 0;
}
// ****************************************************************

int removeAt(int list[], int elements, int searchItem)
{
int y = 0;
int temp = 0;

for (y = 0; y < elements; y++)
{
if (list[y] == searchItem)
{
temp = list[y];
list[y] = list[y + 1];
list[y + 1] = temp;
}
}
return elements - 1;
}
1
2
3
4
5
6
7
8
9
10
11
int removeAt(int list[], int elements, int searchItem)
{
    ...
    --elements;
    for(y = 0; y < elements; y++ )
    {
        ...
    }
    ...
    return elements;
}

You are trying to move the element off the end of the array.
You can't access list[y + 1] when y == elements - 1.
Think about it...
Last edited on
Mathhead how does decrementing elements every iteration give the desired result?
Last edited on
Lol. That's what I get for being rusty... I'll edit it, thanks L B!
Was it really that hard to write y < elements-1? lol.
Thanks for your input guys
You. Rock.
Topic archived. No new replies allowed.