Simple class problem

This is a discussion on Simple class problem within the C++ Programming forums, part of the General Programming Boards category; I am pretty new to C++ and I am trying to write a BlackJack program to help me learn how ...

  1. #1
    Registered User
    Join Date
    Sep 2003
    Posts
    17

    Simple class problem

    I am pretty new to C++ and I am trying to write a BlackJack program to help me learn how to implement classes. So far I only have a Deck class and I am having trouble making it set up the deck and shuffle the cards. I have been able to set up the deck and shuffle the cards by not using classes, so I feel the error is in the way I set up my class or defined the member functions. The program compiles fine, but then kicks out with a generic error message when trying to run it.

    I feel like I have tried everything, but have not been able to make it work. Do I need to use pointers somewhere? Is that the problem? Pointers are definitly a weak suit for me as I have never programmed in a language which uses them before. And I have not really done a lot of programming in any language. I appreciate you taking the time to give me some help.


    Code:
    // This is a blackjack game in c++
    
    #include <iostream>
    #include <string>
    #include <vector>
    #include <algorithm>
    
    using namespace std;
    
    
    // come back and put classes in there own header files
    
    
    class Deck {
    
    	public:
    		vector<string> cards;
    		void shuffle();
    		void set_up_deck();
    	
    	private:
    		string plainCards[52];
    		
    };
    
    
    
    
    
    void Deck::shuffle() {
    	random_shuffle(cards.begin(), cards.end());
    	}
    	
    
    void Deck::set_up_deck() {
    	string plainCards[52] = {"Ace","2","3","4","5","6","7","8","9","10","J","Q","K","Ace","2","3","4","5","6","7","8","9","10","J","Q","K","Ace","2","3","4","5","6","7","8","9","10","J","Q","K","Ace","2","3","4","5","6","7","8","9","10","J","Q","K"};
    	
    	vector<string> cards (52);
    
    	for (int i=0; i<52; i++) {
    		cards[i] = plainCards[i];
    		}
    		
    	}	
    	
    
    
    
    
    int main() {
    
    cout << "Blackjack test" << endl;
    
    Deck deck1;
    
    
    
    deck1.set_up_deck();
    
    
    cout << "Here are the unshuffled cards" << endl;
    for (int j=0; j<10; j++) {
    	cout << deck1.cards[j] << ",";
    	}
    
    
    
    deck1.shuffle();
    cout << "Here are the shuffled cards" << endl;
    for (int j=0; j<10; j++) {
    	cout << deck1.cards[j] << ",";
    	}
    	
    	
    return 0;	
    }

  2. #2
    Senior Member joshdick's Avatar
    Join Date
    Nov 2002
    Location
    Phildelphia, PA
    Posts
    1,146
    I've coded a couple of card games, and I went about it differently. I have a card class that includes the suit and face value of a card. Then, I just throw my cards into whatever container I want. I usually used vectors and queues for that. If I remember correctly, I filled up a vector with cards. Then, I shuffled them in the vector and enqueued them all into a queue to better represent a deck. I don't know if that's the way you want to go, though. Just a thought.
    FAQ

    "The computer programmer is a creator of universes for which he alone is responsible. Universes of virtually unlimited complexity can be created in the form of computer programs." -- Joseph Weizenbaum.

    "If you cannot grok the overall structure of a program while taking a shower, you are not ready to code it." -- Richard Pattis.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Class design problem
    By h3ro in forum C++ Programming
    Replies: 10
    Last Post: 12-19-2008, 09:10 AM
  2. Problem with friend class declaration in a namespace
    By Angus in forum C++ Programming
    Replies: 2
    Last Post: 12-09-2008, 01:29 PM
  3. Default class template problem
    By Elysia in forum C++ Programming
    Replies: 5
    Last Post: 07-11-2008, 09:44 AM
  4. Simple thread object model (my first post)
    By Codeplug in forum Windows Programming
    Replies: 4
    Last Post: 12-12-2004, 11:34 PM
  5. problem with sending files to a class method..
    By utoots in forum C++ Programming
    Replies: 5
    Last Post: 04-02-2003, 01:38 AM

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