1 Replies - 1321 Views - Last Post: 17 March 2013 - 02:44 PM Rate Topic: -----

#1 jibbler  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 20
  • Joined: 05-July 12

2D array getting breakpoint error while running program

Posted 17 March 2013 - 02:01 PM

For today's assignment, I've created a 2D array of integers. It is still a bit crude but the basic logic goes like this:
1. get values from user and enter them into the array
2. call a function that accumulates a running total of all the elements in the array

Upon running the program, I get this error in Visual Studio:
Run-Time Check Failure #2 - Stack around the variable 'pounds' was corrupted.
The thread 'Main Thread' (0x17f0) has exited with code -1073741510 (0xc000013a).
The program '[7292] Monkey Business.exe: Native' has exited with code -1073741510 (0xc000013a).

The program runs and debugs ok, until it reaches line 26 where I get a breakpoint error, and a prompt where it asks me to Break or Continue. I don't understand what the problem is. Any help is appreciated. Can anyone explain what might be wrong?

#include <iostream>

using namespace std;
const int NAMES = 3;
const int DAYS = 7;

void getTotal(int array[][DAYS]);


int main()
{
	int pounds[NAMES][DAYS] = {  };

	for (int a = 0; a < DAYS; a++)
		{
			for (int i = 0; i < NAMES; i++)
				{
					cout << "Please enter the number of pounds consumed by Monkey " << i+1 << " on day " << a+1 << ": ";
					cin >> pounds[a][i];
				}	
			cout << endl;
		} 
		
	getTotal(pounds);
	system("pause");
}

void getTotal(int array[][DAYS])
{
	int total = 0;

	for (int a = 0; a < DAYS; a++)
		{
			for (int i = 0; i < NAMES; i++)
				{
					total+= array[a][i];
				}	
			
		} 
	cout << total << endl;


}


Is This A Good Question/Topic? 0
  • +

Replies To: 2D array getting breakpoint error while running program

#2 jibbler  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 20
  • Joined: 05-July 12

Re: 2D array getting breakpoint error while running program

Posted 17 March 2013 - 02:44 PM

Looks like I made a easy mistake. When filling the array, I accidentally swapped the columns with the rows, and over wrote the array. I cleaned the code up, changed a line and it is working now.

#include <iostream>

using namespace std;
const int NAMES = 3;
const int DAYS = 7;

void getTotal(int array[][DAYS]);



int main()
{
	int pounds[NAMES][DAYS] = {  };

	for (int a = 0; a < NAMES; a++)
		{
			for (int i = 0; i < DAYS; i++)
				{
					cout << "Please enter the number of pounds consumed on day " << i+1 << " by Monkey " << a+1 << ": ";
					cin >> pounds[a][i];
				}	
			cout << endl;
		} 
		
	getTotal(pounds);
	system("pause");
	return 0;
}

void getTotal(int array[][DAYS])
{
	int total = 0;

	for (int a = 0; a < NAMES; a++)
		{
			for (int i = 0; i < DAYS; i++)
				{
					total+= array[a][i];
				}	
			
		} 
	cout << total << endl;
}

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1