Vector subscript out of range

I'm encountering an error in my Uno program, where I am trying to output the deck of cards (excluding wild and wild draw 4 cards at the moment). I'm storing the cards into a vector but when I try to access the last value it causes the error.

How can I fix this by not accessing values outside the vector's range? I've tried using a while loop as well as an if statement to test the vector's size, but that results in an exception handling error.

I suspect the error is in Deck::dealCard() but I can't fix it.

Card.h
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
#ifndef CARD_H
#define CARD_H

#include <iostream>
#include <string>
using namespace std;

class Card
{
public:
	static const int maxCardFaces = 25;
	static const int maxCardColours = 4;
	Card(int cardFace = 1, int cardColour = 1);
	string toString() const;

	int getFace() const
	{
		return face;
	}

	int getColour() const
	{
		return colour;
	}

private:
	int face;
	int colour;
	static const string cardFaces[maxCardFaces];
	static const string cardColours[maxCardColours];
};

#endif 


Card.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
#include "Card.h"
#include "Deck.h"

using namespace std;

const string Card::cardColours[maxCardColours] = { "Blue", "Green", "Red", "Yellow" };
const string Card::cardFaces[maxCardFaces] = { "Zero", "One", "One", "Two", "Two", "Three", "Three", "Four", "Four", "Five", "Five", "Six", "Six", "Seven", "Seven", "Eight", "Eight", "Nine", "Nine", "Draw Two", "Draw Two", "Reverse", "Reverse", "Skip", "Skip"}; //duplicate cards accoring to Uno rules

Card::Card(int cardColour, int cardFace)
{
	colour = cardColour;
	face = cardFace;
}

string Card::toString() const
{
	return cardColours[colour] + " " + cardFaces[face];
}


Deck.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#ifndef DECK_H
#define DECK_H

#include "Card.h"
#include <vector>

using namespace std;

class Deck
{
public:
	Deck();
	void shuffle();
	Card dealCard();
	bool cardsLeft() const;
private:
	vector<Card> deck;
	unsigned currentCard;
};

#endif 


Deck.cpp
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
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <vector>
#include "Deck.h"
#include "Card.h"

using namespace std;

Deck::Deck()
{
	vector<Card> newDeck;

	for (int i = 0; i < Card::maxCardColours; i++)
	{
		for (int j = 0; j < Card::maxCardFaces; j++)
		{
			newDeck.push_back(Card(i, j));
			currentCard++;
		}
	}

	currentCard = 0;
	deck = newDeck;

}

void Deck::shuffle()
{
	for (int i = 0; i < 100; i++)
	{
		int randCard = rand() % 100;

		Card temp = deck[i];
		deck[i] = deck[randCard];
		deck[randCard] = temp;
	}
}

Card Deck::dealCard()
{
		return deck[currentCard++];
}

bool Deck::cardsLeft() const
{
	if (currentCard <= 100)
		return true;
	else
		return false;
}


main.cpp
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
#include <iostream>
#include <string>
#include <iomanip>
#include "Deck.h"

using namespace std;

int main()
{
	Deck myDeck;
	myDeck.shuffle();

	for (int i = 0; myDeck.cardsLeft(); i++)
	{
		cout << left << setw(19) << myDeck.dealCard().toString();
		if (i % 4 == 0)
		{
			cout << endl;
		}
	}

	cin.get();
	cin.ignore();

	return EXIT_SUCCESS;
}
The problem is on line 47 in Deck.cpp: it's supposed to be if (currentCard < 100) // Note: not <=
Thank you so much!
Topic archived. No new replies allowed.