Why is my list not being created?

This is a discussion on Why is my list not being created? within the C++ Programming forums, part of the General Programming Boards category; im trying to make a blackjack game using stacks and lists and i am having a problem with my lists, ...

  1. #1
    Registered User
    Join Date
    Mar 2006
    Posts
    44

    Why is my list not being created?

    im trying to make a blackjack game using stacks and lists and i am having a problem with my lists, its not being created and i dont know y? the list is a list of cards for the players and the stack is the deck of cards.

    Code:
    #include <stack>
    #include <list>
    #include <iostream>
    #include <conio.h>
    #include <ctime>
    #include <windows.h>
    
    using namespace std;
    
    const DECKS = 8;
    #define H char (3)
    #define D char (4)
    #define C char (5)
    #define S char (6)
    
    /**************************************************************/
    struct Card
    {
    	static int counters[52];
    	
    	int val; //0-51
    	int suit,
    		face;
    	
    	Card();
    	~Card() {counters[52]--;};
    	Card (const Card& card);
    	Card& operator = (Card& card);
    	int getSuit() {return suit;};
    	int getFace() {return face;};
    	void print();
    	void printCount() {for(int i = 0; i < 52; i++) cout << counters[i] << endl;}; // delete
    };
    
    Card::Card()
    {
    	val = rand()%52;
    	suit = val / 13;
    	face = val % 13;
    	counters[val]++;
    }
    
    Card::Card(const Card& card)
    {
    	val = card.val;
    	suit = card.suit;
    	face = card.face;
    }
    Card& Card::operator = (Card& card)
    {
    	val = card.val;
    	suit = card.suit;
    	face = card.face;
    	return *this;
    }
    void Card::print()
    {
    	switch(face) 
    	{
    	case 0:
    
    		cout << "Ace";
    		break;
    	case 10:
    
    		cout << "Jack";
    		break;
    	case 11:
    
    		cout << "Queen";
    		break;
    	case 12:
    
    		cout << "King";
    		break;
    	default:
    
    		cout << face+1;
    		break;
    	}
    
    	cout << " of ";
    
    	switch(suit)
    	{
    	case 0:
    
    		cout << H;
    		break;
    	case 1:
    
    		cout << D;
    		break;
    	case 2:
    
    		cout << C;
    		break;
    	case 3:
    
    		cout << S;
    		break;
    	default:
    		break;
    	}
    
    }
    
    /**************************************************************/
    
    class Player 
    {
    private:
    	int plyr;	
    	int cVal; // sum of all the cards
    	list<Card>* hand; // list of cards
    
    public:
    
    	static int players;
    	Player();
    	~Player() {};
    	void recieveCard(Card card);
    	int getPlayer() {return plyr;};
    	Player& operator= (Player& player);
    	bool operator == (Player& player);	
    	Player (Player& player);
    	int calcHand (int cVal);
    	int getHand() {return cVal;};
    	void printHand();
    
    };
    
    Player::Player()
    {
    	cVal = 0;
    	plyr = players++;
    	hand = new list<Card>;
    }
    
    
    bool Player::operator == (Player& player)
    {
    	return plyr == player.plyr;
    }
    
    void Player::recieveCard(Card card)
    {
    	hand->push_front(card);
    }
    
    Player::Player(Player& player)
    {
    	plyr = player.plyr;
    	cVal = player.cVal;
    }
    
    Player& Player::operator= (Player& player)
    {
    	plyr = player.plyr;
    	cVal = player.cVal;
    	return *this;
    }
    
    int Player::calcHand(int cVal)
    {
    	return cVal;
    }
    
    void Player::printHand()
    {
    	cout << cVal << endl;
    	hand->begin();
    	//hand->pop_front();
    }
    
    /**************************************************************/
    
    class Playerlist
    {
    private:
    	int size; //number of players
    	Player* player; // array of players
    
    public:
    	Playerlist();
    	~Playerlist();
    	Playerlist (const Playerlist& playerlist);
    	int getSize() {return size;}
    
    	Player* getPlayer(int index) {return &player[index];}
    };
    
    Playerlist::Playerlist()
    {
    	player = NULL;
    	cout << "How many players will be playing? " << endl;
    	cout << "There is a limit of 5 players at the blackjack table" << endl;
    	cin >> size;
    	
    	if (size > 5)
    	{
    		cout << "You cannot have that many player's; 5 max reenter" << endl;
    	
    	}
    	else
    	{
    	player = new Player [size];
    	}
    }
    
    Playerlist::~Playerlist()
    {
    	delete [] player;
    }
    
    Playerlist::Playerlist (const Playerlist& playerlist)
    {
    	size = playerlist.size;
    	player = new Player [size];
    	for(int s = 0; s < size; s++)
    		player[s] = playerlist.player[s];
    }
    
    /***************************************************************/
    
    int Player::players = 1; //static # of players
    int Card::counters[52] = {0};
    
    
    void main()
    {
    	srand(time(NULL));
    	Playerlist p;
    	list<Card> hand;
        list<Player>::iterator iter;
    	stack<Card> deck;
    	/*char choice1;
    	char choice;*/
    	int k = 0;
    	int l = 0;
    	Card* card;
    	Player* player;
    	
    				player = p.getPlayer(k);
    				cout << "Player " << player->getPlayer() << "'s hand" << endl; 
    				k = ++k % p.getSize();
    
    			for(int i = 0; i < DECKS; i++)
    			{
    				card = new Card;
    				card->print();
    				cout << endl;
    				deck.push(*card);
    			}
    		card->printCount();
    		//do
    		//{
    	while(l < 2)
    	{
    		for(int d = 0; d < p.getSize(); d++)
    		{
    			p.getPlayer(d)->recieveCard(deck.top());
    			deck.pop();
    		}
    		l++;
    	}
    	cout << "Hello" << endl;
    		player->printHand();
    		/*	for(iter = hand.begin(); iter != hand.end(); ++iter)
    			{
    			iter->printHand();
    			}
    			p.getPlayer()->calcHand();
    	  if(player->getHand() == 21)
    		{
    			cout << "You have 21" << endl; 
    			return;
    		}
    	else
    	{
    			while (player->getHand() < 21)
    			{
    			cout << "would you like to hit? y or n" << endl;
    			cin >> choice1;
    			if (choice1 == 'y')
    			{
    				card = new Card;
    				player->recieveCard(*card);
    				//hand.push_back(*card);
    				player->calcHand();
    				if(player->getHand() > 21)
    				{
    					cout << "Bust" << endl;
    					return;
    				}
    				else if(player->getHand() == 21)
    				{
    					cout << "You have 21" << endl; 
    					return;
    				}
    				else
    				{
    					break;
    				}				
    			}
    			else
    			return;
    		
    	} // while
    		} // end else*/	
    		
    
    	/*} // end if player > size
    
    	}
    	//while (choice == 'y'); // end do/while*/
    }// end main
    where i give the player the card

    Code:
    void Player::recieveCard(Card card)
    {
    	hand->push_front(card);
    }
    the print function i am calling

    Code:
    void Player::printHand()
    {
    	cout << cVal << endl;
    	hand->begin();
    	//hand->pop_front();
    }
    ad this is how i implament it

    Code:
    player = p.getPlayer(k);
    				cout << "Player " << player->getPlayer() << "'s hand" << endl; 
    				k = ++k % p.getSize();
    
    			for(int i = 0; i < DECKS; i++)
    			{
    				card = new Card;
    				card->print();
    				cout << endl;
    				deck.push(*card);
    			}
    		card->printCount();
    		//do
    		//{
    	while(l < 2)
    	{
    		for(int d = 0; d < p.getSize(); d++)
    		{
    			p.getPlayer(d)->recieveCard(deck.top());
    			deck.pop();
    		}
    		l++;
    	}
    	cout << "Hello" << endl;
    		player->printHand();
    i thought everything was what it should be

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,806
    Code:
    ~Card() {counters[52]--;};
    52 is an invalid index, valid indicies are from 0 through 51. The last semicolon is not needed. I suspect you mean counters[val]--; instead perhaps?



    Code:
    Playerlist::Playerlist()
    {
        player = NULL;
        cout << "How many players will be playing? " << endl;
        cout << "There is a limit of 5 players at the blackjack table" << endl;
        cin >> size;
    	
        if (size > 5)
        {
            cout << "You cannot have that many player's; 5 max reenter" << endl;
        }
        else
        {
            player = new Player [size];
        }
    }
    What about negative values the user may enter? Any error checking for that? It might make more sense to get the size and do the error checking in the main function and then if the value passes the error checks pass it to the PlayerList constructor as an argument. If the error checking fails, you can then exit the program without needing to go through the rest of the code.


    Code:
    void main()
    main should return an int.



    Code:
    #include <stack>
    #include <list>
    #include <iostream>
    #include <conio.h>
    #include <ctime>
    #include <windows.h>
    
    ...
    
    srand(time(NULL));
    Technically you should be including <cstdlib> as well if you are using srand and rand.



    Code:
    void Player::printHand()
    {
        cout << cVal << endl;
        hand->begin();
        //hand->pop_front();
    }
    The line in red does nothing... it returns an iterator to the first element in the list but this value isn't used in any way so the whole statement serves no purpose. You need to loop through the player's hand.


    Other than that, you aren't updating the cVal data member when receiving a new card, that would seem to be where one would want to do that operation.

    There may be other issues, I haven't done any further checking.
    Last edited by hk_mp5kpdw; 05-04-2006 at 10:13 AM.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    Registered User
    Join Date
    Mar 2006
    Posts
    44
    sorry i didnt copy down far enought.. i did loop through the players hand though

    Code:
    for(iter = hand.begin(); iter != hand.end(); ++iter)
    {
             iter->printHand();
    }
    but i also get errors in that loop

    binary '=' : no operator defined which takes a right-hand operand of type 'class std::list<struct Card,class std::allocator<struct Card> >::iterator'

    binary '!=' : no operator defined which takes a left-hand operand of type 'class std::list<class Player,class std::allocator<class Player> >::iterator'

    and i know im not updating the cVal i have not done that yet but im confised on how to go about doing that too cause thats calculating the value of the players hand
    Last edited by tunerfreak; 05-04-2006 at 10:44 AM.

  4. #4
    Registered User
    Join Date
    Feb 2006
    Posts
    312
    That's because iter is of the type list<Player>::iterator, wheras your list is of the type list<Card> ... you need a list<Card>::iterator

  5. #5
    Registered User
    Join Date
    Mar 2006
    Posts
    44
    ok then i will get an error saying that my print hand function is not a member of Card

  6. #6
    Registered User
    Join Date
    Feb 2006
    Posts
    312
    That's a fairly self-explanatory error. Card doesn't have a member function called printHand() did you mean to type print() ?

  7. #7
    Registered User
    Join Date
    Mar 2006
    Posts
    44
    no i know what the error means but the printHnad is suppose to print the players hand, card dont control that all that print() in the card class does is print what kind of card it is

  8. #8
    Registered User
    Join Date
    Feb 2006
    Posts
    312
    I had assumed the loop you are writing here was part of the printHand() function, looping through your hand container for a Player object - and printing each card in the order that they appear in the container. Obviously i'm wrong, what exactly is this loop supposed to do? and which member function is it from?
    Last edited by Bench82; 05-04-2006 at 04:37 PM.

  9. #9
    semi-colon generator ChaosEngine's Avatar
    Join Date
    Sep 2005
    Location
    Chch, NZ
    Posts
    597
    in class Player, you have a memory leak.
    you have
    Code:
    	list<Card>* hand; // list of cards
    Player()
    {
        hand = new list<Card>;
    }
    ~Player() {}
    you never delete hand. why are you dynamically creating hand anyway? It's doesn't need to be a pointer, just make it a class object.

    Code:
    class Player
    {
         // no need to new or delete it now!
         list<Card> hand;
    };
    it's also the reason this code isn't compiling
    Code:
    for(iter = hand.begin(); iter != hand.end(); ++iter)
    {
             iter->printHand();
    }
    if hand is a pointer, you can't do hand.begin(), you have to do hand->begin().

    but as I said, hand shouldn't be a pointer.
    "I saw a sign that said 'Drink Canada Dry', so I started"
    -- Brendan Behan

    Free Compiler: Visual C++ 2005 Express
    If you program in C++, you need Boost. You should also know how to use the Standard Library (STL). Want to make games? After reading this, I don't like WxWidgets anymore. Want to add some scripting to your App?

  10. #10
    Registered User
    Join Date
    Mar 2006
    Posts
    44
    well i figured it out but list<Card>* hand does need to be a pointer it does not like it otherwise.

    but here is what i came up with to print list

    Code:
    void Player::printHand()
    {
       
       list<Card>::iterator start = hand->begin();  
       list<Card>::iterator stop = hand->end();
       for( ; start != stop; ++start)
    	{
          start->print();  
          cout << endl;
    	}
    	cout << "The value of your hand is: " << cVal << endl;
    }
    then in the main i just call player->printHand() and we are good

  11. #11
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    >> list<Card>* hand does need to be a pointer it does not like it otherwise.

    Just because the current code won't compile when you change it to not be a pointer, doesn't mean it has to be a pointer. It just means you should fix the code that uses it as a pointer to use . instead of ->. Using a pointer there just doesn't make sense, even if it works anyway.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 26
    Last Post: 07-05-2010, 11:43 AM
  2. Simple list code
    By JimpsEd in forum C Programming
    Replies: 1
    Last Post: 05-24-2006, 03:01 PM
  3. instantiated from here: errors...
    By advocation in forum C++ Programming
    Replies: 5
    Last Post: 03-27-2005, 09:01 AM
  4. Linked list with two class types within template.
    By SilasP in forum C++ Programming
    Replies: 3
    Last Post: 02-09-2002, 06:13 AM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 11:21 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21