-
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 */
-
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.
-
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...
-
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.
-
ooooo ok thanks. any ideas why its giving me a warning about the %?
"warning C4552: '%' : operator has no effect; expected operator with side-effect"
-
Code:
playerRankOne % 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.
-
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?
-
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.
-
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 */
-
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).
-
oooo yea I see that now. that randmax function was given to me. I'll check on that. Thanks for the help.