Hi ~

I am getting a strange RunTime Error while debugging my code.

Run-Time Check Failure #2 - Stack around the variable 'player' was corrupted.

Code is below. Would love some help on this one. Thanks.

ps ~ I realize that system("pause") is frowned upon but it is what we were taught to use to keep the console window open. I really just need the error addressed. Thanks Again!

#include<iostream>
#include<iomanip>
#include<cstring>
using namespace std;


const int NAME_LENGTH = 50;

struct Team
{
	char name[NAME_LENGTH];
	double playerNum;
	double points;
};

int main()
{
	const int NUM_PLAYERS = 5;
	Team player[NUM_PLAYERS];
	int index;

	cout << "Enter player names, their numbers and points scored. \n\n";

	for(index = 0; index <= 5; index++)
	{
		cout << "Enter name for player # " << (index + 1) << " : ";
		cin.getline(player[index].name, NAME_LENGTH);
	
		cout << "Enter Player Number for player # " << (index +1) << " : ";
		cin >> player[index].playerNum;
		
	}
		
	system("pause");
return 0;
}

Recommended Answers

All 7 Replies

index <= 5 Your array is only 5 elements long, you should go up to <5 only (0,1,2,3,4)

An error like that typically means you are overrunning the limits of an array and corrupting memory used by other parts of your program or other parts of your system.

const int NUM_PLAYERS = 5;
Team player[NUM_PLAYERS];

//...

for(index = 0; index <= 5; index++) {
}

Your array is only 5-elements long (elements 0-4), but your for loop iterates 6 times (elements 0-5). Get rid of the "equal-to" condition in your loop control.

I dont get the issue you mentioned, but i found if you have a space in the name, then the program goes nuts, to solve this you can try changing the delimiting character to '\n' or something else.

If you can can you use std::string instead of char[] as its much safer and its likely that the char buffer overflowing is what will generate the corruption i would say.

I hope this helps you.

EDIT: as correctly pointed out by the above 2 posts thats definitely the cause of the corruption, can i also add that as well as using only the less than operator, you have defined a constant for your array size, use this constant in your loop so they both scale at the same time. ie

const int cNumElems = 5;

type var[cNumElems];

for( int n = 0 ; n < cNumElems; m++)
{
}
commented: Yes. +5

Thanks! Yes that was definitely the problem. Why would it do that with structures but other programs I have written with arrays only it does not do that when using a for loop?

Also ~ while debugging, the for loop is working fine for the first input, but upon cycling through I am getting weird output:

Enter name for player # 1: jack spratt
Enter Player Number for player # 1: 12
Enter name for player # 2: Enter Player Number for player # 2:

This error continues with each iteration to 5 if you enter a player number. If you enter a character instead you get:

Enter name for player # 1: jack spratt
Enter Player Number for player # 1: 12
Enter name for player # 2: Enter Player Number for player # 2: don
Enter name for player # 3: Enter Player Number for player # 3:Enter name for player # 4: Enter Player Number for player # 4:
Enter name for player # 5: Enter Player Number for player # 5:

Huh?

Thanks!

After you read an integer (12) you still have the ENTER in the buffer (you did press ENTER, didn't you?) After reading integers, you need to clear the input buffer before reading strings and characters.

There is a sticky post that discusses this at the top of the forum.

WaltP's response covers your first question. As he mentioned, you have a lingering newline in your input stream that you have to remove.

As to your second question:
If you enter a character when a numeric input is expected, you corrupt the stream. To correct the situation, you must reset and flush the stream. Both issues are covered in Narue's "How do I flush the input stream?" thread.

Thank you guys! I will definitely check those out. Until then I need to:

cin.ignore(); Correct?

Yes, that fixed both. Man do I feel stupid. I knew that - it's right in the text - but missed it. Thanks for your patience! Still not remembering all the errors and how a program will react (what errors mean what).

ps ~ @WaltP RE: (you did press ENTER, didn't you?) ... I may be blonde but not that blonde although difficult to prove with my question! Thanks Again! I really do appreciate all the help you guys give!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.