Unhanded exception

Hi, Unhanded exception error. Tracing back from the problem function back to main we have.
1
2
3
4
5
6
7
//Functions
void Dealer(string dName[], int dHit[], int i)
{    
	//dName[i+1] = "Dealer";
	//dHit[i+1] = DEALER;
	
}

When the lines are commented the program runs so the issue is here I think.
The function before it is...

1
2
3
4
5
6
7
8
9
10
11
void game(string playersName[],int hitLim[],int i, 
int playerStat[])
{
	
	Dealer(playersName,hitLim,i);
	display();
	displayHit(playersName, hitLim);
	deal(playersName, playerStat);
	limits(playersName, playerStat, hitLim);
	displayEndgame(playerStat, playersName);
}


Which leads back to main.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include "functions2.h"




int main()
{
	int hitL[PLAYERCOUNT]= {0}, cardsT[PLAYERCOUNT] = {0}, 
	index = 0, i = 0;
	string playersname[PLAYERCOUNT];
	char limit = ' ' ;
	ifstream fin("input.txt");

	srand((unsigned)(1234));
	
	//Shuffle cards
	shuffleCards(index);
	
	//Input
	fin >> playersname[i] >> hitL[i];
	//Input
	while(!fin.eof())
	{   
		i++;
		fin >> playersname[i] >> hitL[i];
	     
	}
	
	
	//Game function
	game(playersname,hitL,i,cardsT);

system("pause");


return 0;
}

Hopefully someone can see the issue. I don't understand it. Thanks.
What are the values of local variables at the time of the crash?
You could run the debugger to line 31 and see what i is when you reach it.

[edit: Oops. Your input loop is still incorrect, but my observation about the value of i was as well.]
Last edited on
What are the values of local variables at the time of the crash?

The only way I know how to check this is to do a loop to cout the arrays and then cout i.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
//Functions
void Dealer(string dName[], int dHit[], int i)
{    

	cout << i << endl;
	for(int j = 0; j < PLAYERCOUNT; j++)
	{
		cout << dName[j] << endl;
		cout << dHit[j] << endl;

	}


	//dName[i+1] = "Dealer";
	//dHit[i+1] = DEALER;
	
}


The value of i is 6. This is correct.
dName has the right names and correct number 6 of them.
dHit is correct also. The correct amount of numbers and right values are there. So the problem is with something else maybe? Like i +1 with dealer but I dont; know why that would be.
Last edited on
Has anyone encountered this before?
What is PLAYERCOUNT defined as?
const int PLAYERCOUNT=7
Any ideas?

The function as it is supposed to be which still throws the error is this

1
2
3
4
5
6
7
//Functions
void Dealer(string dName[], int dHit[], int i)
{    
	dName[i+1] = "Dealer";
	dHit[i+1] = DEALER;
	
}


I had the loops above to try and see what was going on...:)
Last edited on
You are going out of bounds of your arrays. If they have 7 elements, and i = 6, then you are accessing array[7] which is out of bounds (valid indices are from 0 to 6 inclusive).
OK but I took that away and it is like one above your last post. This still throws the error. It is supposed to just add to the array because the dealer for black jack is not included in the input file.
Wait, so simply iterating over the arrays using the for loop 6-11 causes it to crash?
I'm not sure I understand you sorry. What happens is I do call stack and it takes me to this function ...

1
2
3
4
5
6
7
//Functions
void Dealer(string dName[], int dHit[], int i)
{    
	dName[i+1] = "Dealer";
	dHit[i+1] = DEALER;
	
}


It has a green arrow which points to
dHit[i+1] = DEALER;
When I compile it I get this error...

Unhandled exception at 0x5c0acac8 (msvcr100d.dll) in mp7.exe: 0xC0000005: Access violation writing location 0xcccccccc.


Edit; 6 to 11 was just to see what the contents were before it was crashing but what I describe above it what was happening. Exacly as is
Last edited on
I thought you said you changed it back to the post on
http://cplusplus.com/forum/general/86988/#msg467002

If you haven't changed it (i.e. it is as written directly above this post) and i is 6 and the size of the arrays is 7, you are going out of bounds on your arrays.
It is exactly like the post above your last. Just forget everything before that. :)
Listen though it doesn't make sense to me because the array is initialized like this
1
2
3
int hitL[PLAYERCOUNT]= {0}, cardsT[PLAYERCOUNT] = {0}, 
	index = 0, i = 0;
	string playersname[PLAYERCOUNT];


Remember PLAYERCOUNT = 7?
So even if i is 6 wouldn't the array still have an extra spot because it was initialized as 7? You see? Or am I wrong?
I made i = 7 before and it didn't throw the error ..you are right .
Indices for arrays start at 0.

So valid indices for an array of size 7 are: 0 to size-1 or 0 to 6 in this case.
OK, I just thought that the size was OK. Guess not.
Thanks now I will try and debug the rest.
Topic archived. No new replies allowed.