Deck was not declared in this scope problem

Hello, I'm trying to get my player1Hand function to work with my deck function so cards can be drawn from the deck and used in each player function. I'm not sure how to call one function from another function because its saying that deck was declared in this scope on line 68:

 
  cout << deck[ z ].rank << " of " << deck[ z ].suit << endl;


Full code:
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
  #include <iostream>
  #include <cstdlib> //for rand and srand
  #include <cstdio>
  #include <string>
  #include <ctime> // time function for seed value
  #include "Card.h"

  using namespace std;



  #define pause cout << endl; system("pause")

  class CardClass {
  public: 

	struct card

  {
	string rank;//this example uses C++ string notation
	string suit;
	int value;
  };
  public:
	void deckFunction ();
	
  private:
	
  };

  void CardClass::deckFunction () {
	srand(time(0));

	struct card deck[52];  // An array of cards named deck, size 52
	

	const string ranks[ ] = { "Ace", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight",
        "Nine", "Ten", "Jack", "Queen", "King" };
		
	const string suits[ ] = { "Diamonds", "Hearts", "Spades", "Clubs" };

	int k = 0; 

	for ( int i = 0; i < 13; i++)
	{
		for ( int j = 0; j < 4; j++)
		{
			deck[ k ].rank = ranks[ i ];
			deck[ k ].suit = suits[ j ];
			k++;
		}
	}
	
   }

   void Players::player1hand () {
	
	CardClass deckFunc;
	deckFunc.deckFunction();
	int p1Chips = 10;
	int pot = 0;
	int player1bet = 0;
	srand(time(0));
	char ans;
	do {
	int z = rand () % 52;

	cout << deck[ z ].rank << " of " << deck[ z ].suit << endl;
	cout << "Place bet: ";
	cin >> player1bet;
	if (player1bet > 0) {
	pot = pot + player1bet;
	p1Chips = p1Chips - player1bet;
	cout << pot << endl;
	cout << p1Chips << endl;
	
	}
	else {
		cout << "Player 1 folds.";
		cin >> ans;
	}
	cout << "Would you like another card? " << endl;
	cin >> ans;
	} while (ans == 'y');
	
   }

	

   int main()
   {
	Players player1;
	player1.player1hand();
	
	pause;

		return 0;

   }

Card.h file:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
   #include <iostream>
   using namespace std;

   class Players
   {
   public:
	void player1hand ();
	void player2hand ();
	void player3hand ();
	void player4hand ();
	void player5hand ();
	void player6hand ();
   private:
	
	
   };




Also, I know the classes are kind of mixed up and sloppy and I shouldn't use using namespace std, but right now I'm just trying to get all the classes and functions to work properly. Thanks!
closed account (D80DSL3A)
I see a variable named deck declared on line 34 in the CardClass::deckFunction ().
It is local to that function and does not exist outside of it.

Is this a homework problem or a personal project?
Yeah, well I need the player1hand to be able to access the card deck because it needs to draw a card. Does the struct need to be passed to player1hand somehow?

Yeah, this is part of a much larger homework assignment, so getting my card class to work with the player hands is crucial before I can move on to the betting system, rules and such.

Thanks for your help!
closed account (D80DSL3A)
You could declare the deck in main() and pass it by reference to deckFunction to initialize it.
Notice how you had to declare an instance of a CardClass object (line 58) just so you could call the deckFunction() function?
Maybe make an INITdeck() function outside of any class instead. Call it from main() to setup the deck. Pass the deck by reference to this function too.

There will be similar problems coming up with other variables local to player1Hand().
How will the pot grow as each player bets?
How do you keep track of each players chips for more than one round of play?

The players class you wrote includes a special function for each player. This isn't how the class model is intended to work.
A class is a model for a single object of some type.
A Player class should model a single player. It would hold data for the player, like his number of chips, cards held in his hand, etc. It would also define methods (functions) for things a Player must do, like getHand(), showHand(), etc.
You might then declare an array of Players in main().

As an example, by no means complete or a best design:
1
2
3
4
5
6
7
8
9
10
11
12
13
class Player
{
public:
    int chips;
    card Hand[maxInHand];// where maxInHand is a constant defined somewhere
    int numCardsHeld;
    bool inGame;
    
    int getHand( card deck[], int& cardsLeft );// returns amount bet?
    void showHand();
private:
    // maybe some of the above members
};

Other issues are apparent. Eg. Use of rand()%52 to select a card from the deck will sometimes result in more than one of the same card dealt. Call random_shuffle() on the deck in INITdeck() then deal the cards in order?
Topic archived. No new replies allowed.