CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    Dec 2007
    Posts
    12

    Newb needs help with structs

    Okay first off I'm sure this probably is some horrible code, but I'm trying to get my feet wet in coding by just creating stuff, no matter how horrible it is.

    But anyway, here's the issue I'm having, I can't get player.cards[] to take on values from the cardIndex[]. I'm sure it's just something that I don't understand between the way classes and/or structs work, but I just can't figure it out. So if anyone could tell me what I'm doing wrong I would greatly appreciate it.

    EDIT: Oh, and I'm not getting compiling errors, my player.cards[] is just holding nothing but gibberish.

    Code:
    #include <iostream>
    #include <stdio.h>
    #include <stdlib.h>
    #include <string>
    #include <time.h>
    using namespace std;
    
    struct card{
    	char face;
    	int value;
    	string suit[10];
    };
    card cardIndex[53] = {
    	{'J', 0, ""},
    	{'A', 1, "Spades"},
    	{'2', 2, "Spades"},
    	{'3', 3, "Spades"},
    	{'4', 4, "Spades"},
    	{'5', 5, "Spades"},
    	{'6', 6, "Spades"},
    	{'7', 7, "Spades"},
    	{'8', 8, "Spades"},
    	{'9', 9, "Spades"},
    	{'J', 10, "Spades"},
    	{'Q', 10, "Spades"},
    	{'K', 10, "Spades"},
    	{'A', 1, "Hearts"},
    	{'2', 2, "Hearts"},
    	{'3', 3, "Hearts"},
    	{'4', 4, "Hearts"},
    	{'5', 5, "Hearts"},
    	{'6', 6, "Hearts"},
    	{'7', 7, "Hearts"},
    	{'8', 8, "Hearts"},
    	{'9', 9, "Hearts"},
    	{'J', 10, "Hearts"},
    	{'Q', 10, "Hearts"},
    	{'K', 10, "Hearts"},
    	{'A', 1, "Clubs"},
    	{'2', 2, "Clubs"},
    	{'3', 3, "Clubs"},
    	{'4', 4, "Clubs"},
    	{'5', 5, "Clubs"},
    	{'6', 6, "Clubs"},
    	{'7', 7, "Clubs"},
    	{'8', 8, "Clubs"},
    	{'9', 9, "Clubs"},
    	{'J', 10, "Clubs"},
    	{'Q', 10, "Clubs"},
    	{'K', 10, "Clubs"},
    	{'A', 1, "Diamonds"},
    	{'2', 2, "Diamonds"},
    	{'3', 3, "Diamonds"},
    	{'4', 4, "Diamonds"},
    	{'5', 5, "Diamonds"},
    	{'6', 6, "Diamonds"},
    	{'7', 7, "Diamonds"},
    	{'8', 8, "Diamonds"},
    	{'9', 9, "Diamonds"},
    	{'J', 10, "Diamonds"},
    	{'Q', 10, "Diamonds"},
    	{'K', 10, "Diamonds"}
    };
    
    
    class Hand{
    public:
    	Hand(int);
    	void givecard(int);
    	int getscore();
    	int getmoney();
    	void getcards();
    	void addmoney();
    private:
    	card cards[10];
    	int i;
    	int money;
    };
    
    Hand::Hand(int start)
    {
    	money = start;
    	i = 0;
    	return;
    }
    void Hand::givecard(int x)
    {
    	cards[i] = cardIndex[x];
    	i++;
    	return;
    }
    void Hand::getcards()
    {
    	int x;
    	for(x=0; x <= i; x++)
    	{
    		cout << "Player has a " << cards[x].face << "of " << cards[x].suit << endl;
    	}
    	return;
    }
    
    void deal(Hand, Hand);
    int pickcard();
    
    int main ()
    {
    	Hand player (500);
    	Hand dealer (0);
    	int bet;
    		deal(player, dealer);
    		player.getcards();
    		getchar();
    
      return 0;
    }
    
    void deal(Hand player, Hand dealer)
    {
    	int i;
    	int cardnumber;
    	for(i=0; i<2; i++)
    	{
    		cardnumber = pickcard();
    		player.givecard(cardnumber);
    		cardnumber = pickcard();
    		dealer.givecard(cardnumber);
    	}
    	return;
    }
    
    int pickcard()
    {	
    	int x;
    	srand((unsigned)time(NULL));
    	x = rand()&#37;52 +1;
    	return x;
    }
    Last edited by Daimones; August 6th, 2009 at 02:11 PM.

  2. #2
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Newb needs help with structs

    First, this should have been posted in the non-Visual C++ forum.

    Secondly, know the difference between pass by value and pass by reference.
    Code:
    void deal(Hand player, Hand dealer)
    What is the difference between what you wrote above, and this:
    Code:
    void deal(Hand& player, Hand& dealer)
    What you wrote means that the compiler will generate a temporary copy of player and hand, work with this temporary copy within the deal() function, and destroy those temporary objects on return. Your original objects are not touched. Is this the desired outcome?

    Regards,

    Paul McKenzie

  3. #3
    Join Date
    Dec 2007
    Posts
    12

    Re: Newb needs help with structs

    Sorry, I guess I was just assuming I was coding this in Visual C++ so I should post it here, my fault.

    Ahh okay, yes I do understand the difference between those, I just was unaware that I was going it. I'm really just starting with classes, so yea.

    But anyway, I passed by reference and things are working much better, of course the suit still is gibberish but I'm sure that's a different issue, I will look into it further before I beg for help. O.o

    Thanks for the help mate.

  4. #4
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,637

    Re: Newb needs help with structs

    Why is suit an array?

  5. #5
    Join Date
    Dec 2007
    Posts
    12

    Re: Newb needs help with structs

    What should it be instead?

  6. #6
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,637

    Re: Newb needs help with structs

    Quote Originally Posted by Daimones View Post
    What should it be instead?
    Looks to me like just a string would do it. No need for an array.

  7. #7
    Join Date
    Jul 2005
    Posts
    266

    Re: Newb needs help with structs

    GCDEF is right suit should be just a string not an array Also you have some other errors that have not been mentioned so far in your code:
    in getcards() your for loop condition is wrong should be just x < i because you index from 0 upwards Another thing is you shouldnt call srand in pickcard() because it forces the rand function to generate the same value, just call it once before the for loop in your deal(Hand&,Hand&) function
    Hope this helps a bit

  8. #8
    Join Date
    Dec 2007
    Posts
    12

    Re: Newb needs help with structs

    Ahh, good point, originally had it as a char array, hence the [] left over from that, forgot to delete them when I decided to use string instead. =)

    Changed the x < i though, had it originally and through troubleshooting trial and error ended up with x<=i, haha. Based on this thread, it seems most of my errors come from randomly trying to troubleshoot stuff, haha. I guess that's just a side effect of working on windows PCs for a living.

    Thanks for all the help guys!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured