Always the BUFFER!

Hi buffer again!
1
2
3
A buffer overrun has occurred in mp7.exe which has corrupted the program's internal state. Press Break to debug the program or Continue to terminate the program.

For more details please see Help topic 'How to debug Buffer Overrun Issues'. 


Call stack take me here for the last thing.
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
#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;

}<=====
Right to the end!

Then this happens in another tab.
The title of the tab is "No source avaliable"
Then it has call stack location kernel32.dll!753e8de()

What is all this about I have gotten buffer overrun with this program for an array which was fixed. But what the heck is this now?
You don't have enough code here to tell anything.

I am curious what shuffleCards does since it doesn't have access to the cardsT array, and I can't think of a good reason it would take an index for an argument.
Could be this
1
2
3
4
5
while(!fin.eof())
	{   
		i++;
		fin >> playersname[i] >> hitL[i];
	}
maybe try this
1
2
3
4
while(fin >> playersname[i] >> hitL[i])
	{   
		i++;		
	}
OK, I will check it out .
Cire! Here is the entire code.

Input
Andy 15
Bob 17
Cathy 16
David 17
Edward 16
Freda 14

//Functions
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
#include "cards.h"

//Card array
cards hand[52];

//Constants

const int PLAYERCOUNT = 7;
const int GAMELIMIT = 21;
const int DEALER = 16; 
const int TWOCARD = 2;


//Functions
void Dealer(string dName[], int dHit[], int i)
{    
	 i = PLAYERCOUNT;
	dName[i+1] = "Dealer";
	dHit[i+1] = DEALER;
	
}

void display()
{
	cout << "BlackJack by Jordan Lillie" << endl;
	cout << "The shuffled deck is : " << endl;

	for(int i = 0; i < 52; i++)
	{
		if(i % 13 == 0)
		{
			cout << endl;
		}

		hand[i].display();
		cout <<' ' << ' '; 

	}
	cout << '\n' << endl;
}

void displayHit(string playersName[], int hitLim[])
{
	cout << "Player Name" << '\t' << "Hit Limit" << endl;
	
	
	for(int i = 0; i < PLAYERCOUNT-1; i++)
	{
		setfill(" ");
		cout << right << setw(11) << playersName[i] << " " 
		<< right << setw(9) << hitLim[i] << endl;
	}
		cout << endl;

}

void deal(string playersName[],int playerStat[])
{
	int counter = 0;
	cout << "Deal two cards to everyone" << endl;
	
	for(int i = 0; i < TWOCARD; i++)
	{
		for( int j = 0; j < PLAYERCOUNT; j++)
		{	
			playerStat[j] += hand[counter++].getFace();
		}

	}
	
	//Display
	for( int k = 0; k < PLAYERCOUNT; k++)
	{
		cout << "Hand for " << playersName[k] << " is : " ;
		hand[k].display();  
		cout << " and " ;
		hand[k + PLAYERCOUNT].display();
		cout << " Value for this hand is " << playerStat[k] << endl;
	}
	 cout << endl << endl;
}

void limits(string playersName[], int playersStat[], int hitLim[])
{

	int hitCount = PLAYERCOUNT * TWOCARD;
	int k = 0;

	for( k = 0; k < PLAYERCOUNT; k++)
	{
		while(playersStat[k] < hitLim[k])
		{
		playersStat[k] += hand[hitCount].getFace();
		cout << "Player " << playersName[k] << " getting another card ";
		hand[hitCount].display();
		cout << endl;
		hitCount++;
		}
	 
		cout << "Value of hand for " << playersName[k] << " is now  " << playersStat[k] << endl;
	}
	
}

void displayEndgame(int playerStat[], string playersName[])
{		
		cout << endl;
		cout << "Results of this game" << endl;

    for(int i = 0; i < PLAYERCOUNT - 1; i++)
	{
		  if(playerStat[PLAYERCOUNT-1] == GAMELIMIT)
	      {
		     for( int k = 0; k < PLAYERCOUNT - 1; k++)
		     {
			 cout << "The deal beats player" << playersName[k] << endl;
		     }
		     break;
	     }
	
	    if(playerStat[i] > GAMELIMIT)
	    {
		    cout << "Dealer beats player " << playersName[i] << endl;
	    }
	else 
	{
	if(playerStat[PLAYERCOUNT-1] <= GAMELIMIT)
    {
			if(playerStat[PLAYERCOUNT - 1] >= playerStat[i])
			{
				cout << "Dealer beats player" << playersName[i] << endl;
			}
			else 
			{
				cout << "Player " << playersName[i] << " beats the dealer" << endl;
			}
	 }
		   else
		   {
			cout << "Player " << playersName[i] << " beats the dealer" << endl;
		   }
	}
 }
	      cout << endl;
}

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);
}

void shuffleCards(int counter)
{
	for(int s = 3; s <= 6; s++)
	{
		for (int f = 1; f <= 13; f++)
		{
			hand[counter] = cards(f, char (s));
			counter++;
		}

	}

	for(int i = 0; i < 52; i++)
	{
		hand[i].swap(hand[(rand() % 52)]);

	}
	cout << endl;
}


class
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#include <string>
#include <ctime>
#include <iomanip>

using namespace std;

class cards
{
  private:
	int face;
	char suit;
	
  public:
	  cards(int = 0, char = ' ');
	  void display();
	  void swap(cards&);
	  void setCards(int, char);
	 
	  //Getters
	  int getFace();
      char getSuit();

};


//Functions
cards::cards(int f, char s)
{
	face = f;
	suit = s;
}

//Swap
void cards::swap(cards& c)
{
	cards temp;

	temp.face = c.face;
	temp.suit = c.suit;
	c.face = face;   
    c.suit = suit;  
    face = temp.face;
    suit = temp.suit;
}

//Display
void cards::display()
{
	switch(face)
	{
	case 11: cout <<'J';
		break;
	case 12: cout << 'Q';
		break;
	case 13: cout << 'K';
	    break;
	case 1: cout << 'A' ;
	    break;
	default: cout << face;
	}
	
	cout << suit;
}

void cards::setCards(int f, char s)
{
	face = f;
	suit = s;
}

int cards::getFace()
{	
	return face;

}

char cards::getSuit()
{
	return suit;
}

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
#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;

}


That's the entire thing no mysteries anymore :)
Hey thanks for the suggestion about the loop cire had suggested that for an earlier buffer problem that was occuring. It still throws the errror. It doesn't help that call stack takes me to the end of main I'm not sure what that means.
1
2
3
4
5
6
void Dealer(string dName[], int dHit[], int i)
{    
	 i = PLAYERCOUNT;
	dName[i+1] = "Dealer";
	dHit[i+1] = DEALER;	
}


Really? In exactly the same place as last time?

Hello out of bounds memory access.

If i is PLAYERCOUNT it is one past the end of the array when used to index the arrays. i+1 is two past the end of the array.
D*MMIT
OK I think I'm not understanding obviously I tried to fix it let me explain my thinking then you can tell me why it is not right. OK, so when I enters that function its value is 6 so I thought that if I want to add something to it I better make the array size one more.
But is it correct to say that
I made i = 7 then I made it == 8 in the i+1 statement? Shouldn't it just be 7?
Because I want to add to it? Is that wrong can I just add one to the array when i = 6 and not make i = PLAYERCOUNT?
Sorry:( I thought I fixed it .
EDIT;
If I make it like this it still does it
1
2
3
4
5
6
7
8
//Functions
void Dealer(string dName[], int dHit[], int i)
{    
	 
	dName[i+1] = "Dealer";
	dHit[i+1] = DEALER;
	
}


If I do

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

Same thing.
I don't think I'm understaing this at all.
Last edited on
It's really very simple. The valid indices for an array are 0 to size-1 where size is the number of elements in the array.

For an array with
1 2 3 4 5 elements, the valid indices are
0 1 2 3 4

Any index higher than 4 (which is size-1 or 5-1) will be out of range.

So, if you already have 7 players in the array and the size of the array is 7, there is no room to store anything else in it. One solution is to make the size of the arrays larger. Another would be to use std::vectors instead of fixed size arrays, so you can grow it to whatever size you need.
Last edited on
I can't use vectors because my teacher hasn't taught that yet :(
It would be easier.
My array is declared like this
1
2
3
int hitL[PLAYERCOUNT]= {0}, cardsT[PLAYERCOUNT] = {0}, 
	index = 0, i = 0;
	string playersname[PLAYERCOUNT];

that my size is 6 or is it still 7 because of how it was declared above?
So if i =6 here does that mean
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
void Dealer(string dName[], int dHit[], int i)
{    
	 i = PLAYERCOUNT;
	dName[i] = "Dealer";
	dHit[i] = DEALER;
	
}

I changed it to this which appears to have solved it 

[code]//Functions
void Dealer(string dName[], int dHit[], int i)
{    
	
	
	dName[i] = "Dealer";
	dHit[i] = DEALER;
	
}


When I display still my values are all off. That is done by the the deal function. If I a cout to see the contents of the hand array the I get some werid weird values.
If i is 6 when you get to Dealer and the size of the arrays is 7, the arrays are full since i was set to the last index you read into.

I'm not terribly surprised about the hand array since you're using control characters to represent the suits.
See. I didn't know how to shuffle my cards in my class example the cards are displayed as
JH and QH and so on. But I got stuck on how to do this is I looked at my labaids website and he did it displaying the cards from ascii table. Now I don't really want this. I don't know how I would solve it still because I don't know how to get just H= heat C= club just the card types? I think that my cards array is not being distinguished. The problem with the hand value is that when you shuffle the deck, I initialize the face cards to values 11-13, which I need to distinguish what they are. But when I use them to add up a score it's not correct. Do you know a way to get my cards so they are JH and QC instead of the char characters in ascii. And How can I fix that my array values are wrong with hand.
What's wrong with control characters cire? First eof now control characters. haha. I'm just joking but seriously whats wrong with them and how do I fix my array values?
Topic archived. No new replies allowed.