Accessing Elements of Vectors within Vectors Involving Class Point

The program is suppose to store a hand of 7 cards each for 5 players after which each player must remove a card and pass to the next until all of a player's cards are the same suit.

I seem to be having difficulties accessing a vector within a vector that is using class. It would be nice if someone could find the error.

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
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <string>
#include <algorithm>
#include <vector>

using namespace std;

class card{//1st Constructors and initializers
	char m_char; // "S" "C" "D" "H"
	int m_num;
public:
	card(const char s = 'S', const int n = 0) : m_char(s), m_num(n){/*2nd*/};
	friend ostream& operator << (ostream& o, const card& c);
};

ostream& operator << (ostream& o, const card& c){
	o << c.m_char;
	switch(c.m_num){
	case 1: o << "A"; break;
	case 11: o << "J"; break;
	case 12: o << "Q"; break;
	case 13: o << "K"; break;
	default: o << c.m_num;
	}
	return o;
}

class player {
	string m_name;
	vector<card> m_deck;
public:
	player(const string& n) : m_name(n) {m_deck.reserve(7);}
	void push_back(const card& c){m_deck.push_back(c); }
	void back(){m_deck.back();}
	friend ostream& operator<<(ostream& o, const player& p);
};

ostream& operator<<(ostream& o, const player& p){
	o << "Player: " << p.m_name << " has: " <<endl;
	for (int i = 0, len = p.m_deck.size() - 1; i < len; ++i){
		char a = 'a';
		o << (char)('a' + i) << ") " << p.m_deck[i] << " ";
	}
	return o;
}


int main(){
	//Deck Building
	vector<card>deck(52);
	for (int i = 1; i <= 13; ++i) deck.push_back(card('S', i));
	for (int i = 1; i <= 13; ++i) deck.push_back(card('C', i));
	for (int i = 1; i <= 13; ++i) deck.push_back(card('D', i));
	for (int i = 1; i <= 13; ++i) deck.push_back(card('H', i));
	random_shuffle(deck.begin(), deck.end());

	int hcount, ccount, dcount, scount; hcount = ccount = scount = dcount = 0;
	//Creating Players and Thier "Hand" Vectors
	vector<player>players;
	players.push_back(player("You"));
	players.push_back(player("Player1"));
	players.push_back(player("Player2"));
	players.push_back(player("Player3"));
	players.push_back(player("Player4"));

	//Filling Eaching "Hand" Vector with 7 Cards
	for (int i = 0, len = players.size(); i < len; ++i){
		for (int j = 0; j < 8; ++j){
			card c = deck.back();
			players[i].push_back(c);
			deck.pop_back();
		}
	}

	while(true){
		cout << players[0] << endl;
		cout << "What to Do: ";
		string input; cin >> input;
		if (input == "?") {
			for(int i = 1, len = players.size(); i < len; ++i) cout << players[i] << endl;
		}
		if (input == "quit") break;
		//Creating a counter for the Cards
		for (int i = 0, len = players.size(); i < len; ++i){
					if (players[i] == 'H') ++ hcount;
					else if (players[i] == 'C') ++ccount;
					else if (players[i] == 'D' ) ++dcount;
					else if (players[i] == 'S' ) ++scount;
				}
		cout << hcount << ccount << dcount << scount;
		card hold;


		if(input=="a") {hold = players.back(); players[6] = players[0]; players[0] = hold;}
		else if(input=="b") {hold = players.back(); players[6] = players[1]; players[1] = hold; }
		else if(input=="c") {hold = players.back(); players[6] = players[2]; players[2] = hold; }
		else if(input=="d") {hold = players.back(); players[6] = players[3]; players[3] = hold; }
		else if(input=="e") {hold = players.back(); players[6] = players[4]; players[4] = hold; }
		else if(input=="f") {hold = players.back(); players[6] = players[5]; players[5] = hold; }
		else if(input=="g") {hold = players.back(); players[6] = hold; }



	}
	return 0;
}



if(input=="a") {hold = players.back(); players[6] = players[0]; players[0] = hold;} is creating a syntax error. Also, I am having trouble with the card counter code.

The rest of the code works fine when i comment out these two pats of code.
Last edited on
Topic archived. No new replies allowed.