code error vector subscript

Error: occurs after 4 numbers(representing cards) have been played.

Expression:vector subscript out of range

Please help me i've been working on this for weeks
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
#include<iostream>
#include<vector> 
using namespace std;
 
void intro()
{
	//gives the instructions to the players for the game 
	cout<<"welcome to the game of war!"<<endl;
	cout<<"I shall shuffle the deck and deal the cards so that both of you will receive 26 cards."<<endl; 
	cout<<"Your cards will appear as numbers 2-10 are number cards, jack=11, queen=12, king=13, ace=14"<<endl; 
	cout<<"There will be no suits for these cards because only the number on the card is of any value to you"<<endl; 
	cout<<endl<<"You will keep your cards in your deck, you do not get to see them."; 
	cout<<"Each of you will be prompted to place down a card and the higher card is the winner.";
	cout<<"The winning player receives both cards and they will be placed in the bottom of your deck."<<endl; 
	cout<<"Your cards will occasionally be automatically shuffled"<<endl; 
	cout<<endl<<"In the event of a tie a war shall begin!"<<endl; 
	cout<<"The player with the higher of these cards takes all 10 cards which are now in the middle."; 
	cout<<"If these two cards are also a tie, additional cards are turned face up, one at a time, until one player wins."; 
	cout<<"That player takes the entire set of cards."<<endl; 
	cout<<"When one player has all 52 cards they are the winner!"<<endl; 
	cout<<endl<<"If you wish to play again you can choose to do so after one player has won the deck"<<endl;
	cout<<endl;
	cout<<endl;
	cout<<"THE WAR HAS BEGUN!"<<endl;
	return;
}

void shufflecards (vector<int>&shuffledeck)
{
	//takes in a single deck and mixes up the cards
	for (int i=0; i<shuffledeck.size()-1; i++)
	{
		int r = rand() % shuffledeck.size()-1;
		int temp = shuffledeck[i];
		shuffledeck[i]= shuffledeck[r];
		shuffledeck[r] = shuffledeck[temp];
	}
	//passes back the cards to the deck through a reference parameter
}

void PlayerOneDeck (vector<int>&deckone)
{
	
	//for(int z=0; z<deckone.size(); z++)		//for debugging purposes in case the deck is wrong -AV
	//{
	//	cout << deckone[z] << endl;
	//}
	if(deckone.size()<26) 
	{
		shufflecards(deckone);
	}

	//stores the first players deck so it can be accessed to put in and take out cards
}

void PlayerTwoDeck (vector<int>&decktwo)
{
	
	//for(int z=0; z<decktwo.size(); z++)		//for debugging purposes in case the deck is wrong -AV
	//{
	//	cout << decktwo[z] << endl;
	//}
	if(decktwo.size()<26)
	{
		shufflecards(decktwo);
	}
	//stores the second players deck so it can be accesses to put in and take out cards
}

int finditemindex (vector<int>&deckie, const int goal)
{
	for(int h=0; h<deckie.size(); h++)
	{
		if(deckie[h]==goal)
		{
			return(h);
		}
		return(-1);
	}
}

void snip(vector<int>&cuthand, const int &valtocut)
{
	int index=finditemindex(cuthand, valtocut);
	for(; index < cuthand.size()-1; index++)
	{
		cuthand[index]=cuthand[index+1];//allows card to be taken out and given to the other player 
	}
	cuthand.resize(cuthand.size()-1); //resizes hand to hold less cards
	
}

void warsnip(vector<int>&cuth, const int &valcut)
{
	int indexwar=finditemindex(cuth, valcut);
	for(; indexwar < cuth.size()+3; indexwar++)
	{
		cuth[indexwar]=cuth[indexwar+3];//allows card to be taken out and given to the other player 
	}
	cuth.resize(cuth.size()-3); //resizes hand to hold less cards
	
}

void war (vector<int>&card1, vector<int>&card2)
{
	int inp1,inp2;
	//when the two players come up with the same card in the play function it will trigger war()
	//the players will be prompted that a war has broken out.
	cout<<"there has been a war!"<<endl;
	cout<<"both of you will place down the fourth card down to see who wins. The winner recieves not only both cards but 3 cards from the others deck."<<endl;
	//both players place the fourth card face up.

	cout<<"player one to put down a card enter a 1"<<endl;
	cin>>inp1;
	if(inp1==1)
	{
		cout<<card1[0]<<endl;
	}
	cout<<"player two to put down a card enter a 1"<<endl;
	cin>>inp2;
	if(inp2=1)
	{
		cout<<card2[0]<<endl;
	}
	//the higher card of the two wins and receives both cards played
	if (card1[0]>card2[0])
	{
		card1.resize(card1.size() +1);
		card1[26]=card2[0];
		warsnip(card2, card2[0]);
		//move both cards into deckun
	}
	if(card1[0]<card2[0])
	{
		//move both cards into deckdu
		card2.resize(card2.size() +1);
		card2[26]=card2[0];
		warsnip(card1, card1[0]);
	}
}

void play(vector<int>deckun, vector<int>deckdu)
{
	do
	{
		int in1, in2;
		//takes in both players decks so cards can be used from them to play 
		
		PlayerOneDeck(deckun);
		PlayerTwoDeck(deckdu);
		int numcard1= deckun.size();
		int numcard2= deckdu.size();

		//each player will be prompted to place a card down 
		cout<<"player one to put down a card enter a 1"<<endl;
		cin>>in1;
		if(in1==1)
		{
			cout<<deckun[0]<<endl;
		}
		cout<<"player two to put down a card enter a 1"<<endl;
		cin>>in2;
		if(in2==1)
		{
			cout<<deckdu[0]<<endl;
		}
		//the higher card of the two wins and receives both cards played
		if (deckun[0]>deckdu[0])
		{
			deckun.resize( deckun.size() +1 );
			deckun[26]=deckdu[0];
			snip(deckdu, deckdu[0]);
			//move both cards into deckun
		}
		else if (deckun[0]<deckdu[0])
		{
			//move both cards into deckdu
			deckdu.resize(deckdu.size() +1);
			deckdu[26]=deckdu[0];
			snip(deckun, deckun[0]);
		}
		else if (deckun[0]=deckdu[0])
		{
			war(deckun, deckdu); //trigger a war
		}
	}
	while (deckun.size() != 0 && deckdu.size() != 0);
	char newgame;
	if (deckun.size()==0)
	{
		cout<<"Player Two you Win!!";
		cout<<" Play again? (y for yes, n for no) "<<endl; //this will end the game once the deckun reaches zero
		cin>>newgame<<endl;
		if (newgame=='y')
		{
			main();
		}
		else if (newgame=='n')
		{
			cout<<"Fare thee well ogre"<<endl;
		}
		else 
		{
			cout<<"BAKA!!"<<endl;
		}
	}
	else if (deckdu.size()==0)
	{
		cout<<"Player One You Win!!";
		cout<<" Play again? (y for yes, n for no) "<<endl; //this will end the game once the deckdu reaches zero
		cin>>newgame<<endl;
		if (newgame=='y')
		{
			main();
		}
		else if (newgame=='n')
		{
			cout<<"Fare thee well ogre"<<endl;
		}
		else 
		{
			cout<<"BAKA!!"<<endl;
		}
	}
	else
	{
		cout<<"Inconvievable!";
	}
}

void deal(vector<int>deckboss)//takes in the deck function and places it into deckboss
{
	//takes in cards from deck
	vector<int>deck1(26);
	vector<int>deck2(26);
	int b=deckboss.size();
	shufflecards(deckboss);

	//passes deck to shuffle the cards before they are shuffled
	bool grr = true;
	int counter=0;
	for (int y=0; y<b; y++)//decrements through the entire deck. 
	{ 
		if (grr==true)//gives 26 numbers starting from deckboss[0] (even card positions within the deck)
		{
			deck1[counter] = deckboss[y];
			grr=false;
		}
		else if (grr==false)//gives 26 numbers starting from deckboss[1] (odd card positions within the deck)
		{
			deck2[counter]=deckboss[y];
			grr=true;
			counter++; //changes the deck count for deck1 and deck2 changing the position you place each card
		}
	}
	PlayerOneDeck(deck1);
	//passes the first deck to PlayerOneDeck()
	PlayerTwoDeck(deck2);
	//passes the second deck to PlayerTwoDeck()
	play(deck1, deck2);
	//passes the decks to play()
}

void deck(vector<int>begin)
{
	int j=2;
	int u=1;
	vector<int>cards(52);
	//creates and stores the 52 cards that make up a deck
	for (int i=0; i<52; i++)
	{
		while(j>u && j<15)
		{
			cards[i]=j;
			cards[i+1]=j;
			cards[i+2]=j;
			cards[i+3]=j;
			i=(i+4);
			j++;
		}
	}
	/*for(int z=0; z<cards.size(); z++)		//for debugging purposes in case the deck is wrong -AV
	{
		cout << cards[z] << endl;
	}*/
	deal(cards);
}

int main() //calls the game into play one function at a time
{
	vector<int>start(52);
	intro();
	deck(start);

	return(0);

}
Last edited on
Hi there,

If you get any errors, please include them in your post.

The Error lies within snip vector<int>finditemindex(cuthand, valtocut); and warsnip vector<int>finditem(cuth,valcut);


That's because those functions don't exist.
What you need is this function: http://cplusplus.com/reference/algorithm/find/

That will return an iterator to the value you need, which you can then use from there onwards.
http://cplusplus.com/reference/iterator/

Hope that helps.

All the best,
NwN
Thank you I figured it out though, no more error messages in my code!
I think the issue may be within the play function it doesnt trigger if one card is bigger than the other
Topic archived. No new replies allowed.