//prototypes...
void play21(void);
int dealCards(int, string);
void hit(int &);
void determineWinner(int, int);
int Random(int, int);
void main(){
char keepPlaying = 'n'; //loop control variable
do {
play21();
//keep playing?
cout << "Do you want to play anouther hand (y/n)?";
cin >> keepPlaying;
} while(keepPlaying == 'Y' || keepPlaying == 'y');
}
void play21(void){
//play one hand of 21.
//randomize the cards.
srand((int) time(0));
// deal the cards.
int person = dealCards(2, "Your Cards:");
cout << " = " << person << endl;
int house = dealCards(2, "Computers Cards:");
cout << " = " << house << endl;
// Ask if human wants a hit and keep hitting...
hit(person);
cout << endl;
//Determine if computer takes a hit.
while ((house < person) && (house <= 21) && (person <= 21)) {
house += dealCards(1, "The Computer takes a card ");
cout << endl;
}
//show who won....
determineWinner(person, house);
}
void determineWinner(int humanScore, int houseScore) {
while ((humanScore <= 21) && (humanScore < 0)) //Compare the scores.
if (humanScore == 21)
{
cout << "You have 21. You win!" << endl;
}
else if ((humanScore < 21) && (humanScore > houseScore))
{
cout << "You have the closer hand to 21. You win!" << endl;
}
//outcomes: human wins, computer wins, tie.
}
int dealCards(int numberOfCards, string message){
int sumOfCards = 0;
for (int a = 0; a <= numberOfCards; a++) //This function deals the cards.
{
//Random();
return sumOfCards;
}
1) Try to use code tags. They help others in reading your code.
2) infinite loops can only appear where there are looping like functions. For example
Code:
for(int a = 5; a > 2; a++)
will loop forever because it will never meet the breaking conditions (a >2)
Do you see any loops in your code in which the breaking condition is never met?
Sort of. It's not a "breaking condition", it's continue looping while the expression is true. In this case, a will be > 2 until it overflows, so it will loop for a long time.
Thanks for the correction. I wasn't thinking about nomenclature and I didn't think about the overflow later on. I typically stop it after it re-iterates my code about 10 times more than it should and call it infinite!
Decipil, what I should have said was functions such as FOR and WHILE continue to loop while a certain expression is true (in my previous example the expression was a > 2). Your textbook should be able to show you how to structure both types of loops and also show how and where the expression goes to control the loop
Sorry about the indentions, when I posted it I thought it showed them, but what might be the problem with that piece of code, I have been working on for a while now and can't seem to figure out what is wrong with it.
Sorry about the indentions, when I posted it I thought it showed them, but what might be the problem with that piece of code, I have been working on for a while now and can't seem to figure out what is wrong with it.
In C++, anything that evaluates to non-zero is true. 'y' is not zero, therefor that expression is always true.
You really meant
while (anotherCard == 'Y' || anotherCard == 'y')
I don't know if that's your only problem, but it's certainly one of them.
To maintain indentations put [ code ] before your code and [ /code ] after, but don't include the spaces before and after the brackets.
Sweet deal, thanks a load I really appreciate it, that solved the problem with the loop, now all I got to do is figure out how to get the random numbers to work instead of just showing zeros.
Bookmarks