I am trying to write a blackjack program. The only function I can't get to work is the hit function which determines whether a player wants to hit or stay. If i enter "hit" or "h" when prompted, the program works as expected. But if i enter "stay" or "s", the program treats it as if playerHits is still null, asking me to enter hit or stay. playerHits is a global bool that isn't modified by any other function.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
void hit(){
playerHits=NULL; //whether player wants to hit
cout<<"\nWould you like to [h]it or [s]tay?"<<endl;
while(playerHits==NULL){
string s="";
getline(cin, s);
cin.ignore();
if(s.compare("hit")==0||s.compare("h")==0)
playerHits=true;
if(s.compare("stay")==0||s.compare("s")==0)
playerHits=false;
if(playerHits==NULL)
cout<<"\nPlease enter [h]it or [s]tay."<<endl;
}
}
I am trying to get the user to input either hit or stay. The while loop ensures that the user enters one of these before exiting the function. However, for some reason my program won't accept "stay" or "s" as acceptable inputs either.
Ya that's it. I created another variable to be set to true if "s" or "stay" is entered and that fixed it completely. I didn't realize that in C++ NULL was equivalent to 0 which ends up being evaluated as false in a boolean. Thanks for the help.