Thread: blackjack simulator

  1. #1
    Registered User
    Join Date
    Feb 2008
    Posts
    10

    blackjack simulator

    Trying to code a blackjack simulator. just kinda practicing with this code to get a handle on what Im going to have to do, but i keep getting errors about the "%" operand and an uninitialized varible for playerRankOne.

    Code:
    #include <iostream>
    #include <cstdlib>
    
    using namespace std;
    
    int randmax (int);
    
    int main()
    {//main
    	
    	int handCount = 0;
    	int cardMax = 13;
    	int suit = 4;
    	int playerRankOne;
    	
    	cout << "=========================================" << endl;
    	cout << "    Welcome to the Blackjack Simulator   " << endl;
    	cout << "=========================================" << endl;
    
    	cout << "Hand 1" << endl;
    
    	while (handCount < 21)
    	{//while loop til 20 hands
    		randmax (playerRankOne);// get random #
    		playerRankOne % cardMax;// make random # 1-13
    		switch (playerRankOne)
    		{// print Ace King Queen or Jack for the values 1, 13, 12, 11 
    		case 1 : cout << 'A' << endl;
    		case 11 : cout << 'J' << endl;
    		case 12 : cout << 'Q' << endl;
    		case 13 : cout << 'K' << endl;
    		default : cout << playerRankOne << endl; // or the number if not AKQJ
    		}// end switch for AKQJ
    		handCount++;
    		cout << "Hand " << handCount << endl;
    	}//end while loop
    		return 0;
    
    }//main
    
    int randmax (int max)
    { /* randmax */
    	static int seed = 2; 
    	seed = (seed * 13077 + 6925) % 32768;
    	return (static_cast<int> (max * seed / 32768.0 + 1.0));
    } /* randmax */

  2. #2
    Registered User
    Join Date
    Dec 2007
    Posts
    214
    In your function call

    randmax (playerRankOne);

    playerRankOne has no value yet, at least no good value, just gargage. You declare it above, but you don't initialize it.

  3. #3
    Registered User
    Join Date
    Feb 2008
    Posts
    10
    I thought I was sending the variable into the randmax function to get some random value that I would then modulus into the card. I didn't think it needed to have a value before being sent to the function...

  4. #4
    Registered User
    Join Date
    Dec 2007
    Posts
    214
    Look what the randmax function is doing. Your input int is max in that function. max is multiplied by seed within the randmax function, but it has no known value there.

  5. #5
    Registered User
    Join Date
    Feb 2008
    Posts
    10
    ooooo ok thanks. any ideas why its giving me a warning about the &#37;?

    "warning C4552: '%' : operator has no effect; expected operator with side-effect"

  6. #6
    Registered User NeonBlack's Avatar
    Join Date
    Nov 2007
    Posts
    431
    Code:
    playerRankOne &#37; cardMax;
    You are doing this division and then throwing away the result. The compiler wants you to do something with it (like assignment it to something).

    edit: Avoid useless comments like //begin main function
    and such. Anyone who looks at the code knows what the main is, what a while loop does etc...
    Also, it's not a big issue, but I think a for loop would be more appropriate than a while in this case.
    Last edited by NeonBlack; 03-20-2008 at 01:30 PM.
    I copied it from the last program in which I passed a parameter, which would have been pre-1989 I guess. - esbo

  7. #7
    Registered User
    Join Date
    Feb 2008
    Posts
    10
    gotcha, it compiles with no warnings now, but nothing is returning from the randmax function. I stepped through it with the debugger, and it returns a value of 1 for some reason I can't figure out. any thoughts?

  8. #8
    Registered User NeonBlack's Avatar
    Join Date
    Nov 2007
    Posts
    431
    I don't know what you mean by "nothing is returning from it"
    Code:
    return (static_cast<int> (max * seed / 32768.0 + 1.0));
    randmax is returning whatever max * seed / 32768.0 + 1.0 happens to be. But since playRankOne (which becomes max) is not initialized, it could be anything. Set playRankOne to some number and see if it produces the results you expect.

    Code:
    randmax (playerRankOne);
    randmax is returning a value, but again, you're just throwing it away. You might want to assign it to a variable.
    Last edited by NeonBlack; 03-20-2008 at 02:51 PM. Reason: fixed code tag
    I copied it from the last program in which I passed a parameter, which would have been pre-1989 I guess. - esbo

  9. #9
    Registered User
    Join Date
    Feb 2008
    Posts
    10
    randmax is returning a value of 1 for some reason now. heres what I've done to it since that first post
    Code:
    #include <iostream>
    #include <cstdlib>
    
    using namespace std;
    
    int randmax (int);
    
    
    int main()
    {
    	
    	int handCount = 1;
    	int cardMax = 13;
    	int suit = 4;
    	int playerRankOne;
    	
    	
    	cout << "=========================================" << endl;
    	cout << "    Welcome to the Blackjack Simulator   " << endl;
    	cout << "=========================================" << endl;
    
    	cout << "Hand 1" << endl;
    
    	while (handCount < 21)
    	{//while loop til 20 hands
    		randmax (cardMax);// get random #
    		playerRankOne = cardMax % 13 ;// make random # 1-13
    		switch (playerRankOne)
    		{// print Ace King Queen or Jack for the values 1, 13, 12, 11 
    		case 1 : cout << 'A' << endl;
    		case 11 : cout << 'J' << endl;
    		case 12 : cout << 'Q' << endl;
    		case 13 : cout << 'K' << endl;
    		default : cout << playerRankOne << endl; 
    		}
                     handCount++;
    		cout << "Hand " << handCount << endl;
    	}
    		return 0;
    
    }
    
    int randmax (int max)
    { /* randmax */
    	static int seed = 2; 
    	seed = (seed * 13077 + 6925) % 32768;
    	return (static_cast<int> (max * seed / 32768.0 + 1.0));
    } /* randmax */

  10. #10
    Registered User NeonBlack's Avatar
    Join Date
    Nov 2007
    Posts
    431
    If max * seed / 32768.0 is a number less than 1 (it probably is- I don't have my calculator with me), then max * seed / 32768.0 + 1.0 is a number between 1 and 2. When you cast to int, the fractional part gets chopped off and you return 1.
    If you're not familiar with how integers work in C, take a look at the faq. But basically casting to int is the same as rounding down to the nearest integer (floor function).
    I copied it from the last program in which I passed a parameter, which would have been pre-1989 I guess. - esbo

  11. #11
    Registered User
    Join Date
    Feb 2008
    Posts
    10
    oooo yea I see that now. that randmax function was given to me. I'll check on that. Thanks for the help.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simple Blackjack Program
    By saber1357 in forum C Programming
    Replies: 1
    Last Post: 03-28-2009, 03:19 PM
  2. need help with cache simulator!
    By dtogers123 in forum C Programming
    Replies: 3
    Last Post: 04-30-2008, 06:18 PM
  3. Help with Blackjack program
    By sugie in forum C++ Programming
    Replies: 1
    Last Post: 04-30-2005, 12:30 AM
  4. destroywindow() problem
    By algi in forum Windows Programming
    Replies: 6
    Last Post: 03-27-2005, 11:40 PM
  5. Blackjack!
    By Dr. Bebop in forum Game Programming
    Replies: 1
    Last Post: 10-03-2002, 08:58 PM