c++数学问题

C++ mathematical issue

本文关键字:问题 c++      更新时间:2023-10-16

所以在我的最后一个项目中,一个使用纸牌类继承的黑杰克和扑克模拟器,我们必须跟踪用户的赌注和总金额。然而,在我的代码中,它做了非常奇怪的事情。例如:如果你的总资金是1000,000,000美元,你下注100,赢回200,你的新总资金现在等于199美元

我的程序在做这个和不做这个之间来回切换。这太让人抓狂了,我不知道为什么会这样。下面是我的主要函数,以及处理每个扑克游戏的两个函数。如果有人认为需要更多的代码来回答,尽管我很乐意包括类头文件和实现文件。感谢所有可能提供帮助的人!下面是我的主要函数,以及处理每个游戏的两个函数:

unsigned int handlePoker(unsigned int);
unsigned int handleBlackJack(unsigned int);
//main function:
//asks the user what game they want to play
//then calls a function for the appropriate
//game chosen
int main()
{//two choices:
//one for quitting the program
//the other for whichever game they want
    char yesOrNo;
    char choice;
    unsigned int totalMoney;
    cout<< "please enter a starting amount to bet with"<<endl;
    cin>>totalMoney;
    cout<<"would you like to play?"<<endl;
    cout<<"enter 'y' for yes and 'n' for no"<<endl;
    cin>>yesOrNo;
    do{
        //ask the user which game they want
        cout<<"would you like to play poker or black jack?"<<endl;
        cout<<"input '1' for poker and '0' for blackjack"<<endl;
        cin>>choice;
        if(choice == '1')
        {
            totalMoney = handlePoker(totalMoney);
        }//end if
        else if(choice == '0')
        {
            totalMoney = handleBlackJack(totalMoney);
        }//end else if
       else
       {
            cout<<"I'm sorry, the input you entered was invalid"<<endl;
            cout<<"please try again"<<endl;
            cin.clear();
        }//end else
        cout<<"would you like to try again?"<<endl;
        cout<<"('y' for yes, or 'n' for no)"<<endl<<endl;
        cin>>yesOrNo;
   }while(yesOrNo == 'y' || yesOrNo == 'Y'); //end do while loop
   return 0;
}//end int main

//handle poker:  
//a void function which takes an
//unsigned integer value
//the function declares a "poker" object
//and uses it's void functions to sim a poker game
unsigned int handlePoker(unsigned int tot)
{
    unsigned int multiply;
    unsigned int betMonies;
    unsigned int win;
    poker round;
    cout<<"how much do you want to bet?"<<endl;
    cin>>betMonies;
    //if the bet money entered was valid
    // we begin playing
    if(betMonies < tot)
    {//big if begin
        //ask if they want a better hand
        round.betterHand();
        //set the number we multiply your money by 
        multiply = round.rewardMoney();
        //if multiply is 0
        //then the user has lost this hand
        //we inform them as such, and subtract
        //their bet money from their total money
        if(multiply == 0)
        {
            cout<<"I apologize, but you seem to have lost"<<endl;
            cout<<"when you lose, your bet is subtracted"<<endl;
            cout<<"your initial balance was: "<<tot<<endl;
            //decrement the total
            tot = (tot - betMonies);
            cout<<"your new balance is: "<<tot<<endl;
        }//end if
    //if multiply is not 0 (assuming it's not negative
    //because there's no way it could be)
    //we tell them what they've won, and add it to
    //their total money
   else
   {
        win = (multiply*betMonies);
        cout<<"your initial balance was: "<<tot<<endl;
        cout<<"your win was"<<win<<endl;
        //increment the total
        tot = (win + tot);
        cout<<"your new balance is "<<tot<<endl;
    }//end else
}//big if end
//if the amount entered was not valid
//simply tell them, then run the loop again
else
{//else begin
    cout<<"I'm sorry, that was not a valid amount of money"<<endl;
    cout<<"please try again"<<endl;
}//end else
round.shuffleDeck();
return tot;
}//end handlePoker

//handle Black jack:
//a function returning an unsigned int
//that keeps track of the total money
//declares a black jack object
//and uses it's member functions to play black jack
unsigned int handleBlackJack(unsigned int tot)
{
blackJack play;
unsigned int reward;
unsigned int betMoolah;
//ask the user for the bet they want
cout<<"how much do you want to bet?"<<endl;
cin>>betMoolah;
//if the bet is less than the total passed by reference
//then we can start running the game 
if(betMoolah < tot)
{
    //print the hands dealt in the constructor
    play.printHands();
    //run the function that lets them hit or stay
    //the function contains a do while loop
    // so, no looping is required
    play.hitOrStay();
    //we then handle the reward
    //which returns an integer type
    reward = play.handleReward(betMoolah);
    //prints dealer and player's hands fully
    play.printHandGame();
    //in one of the cases, reward is set to -1
    //we use this here:
    if(reward < 0 )
    {
        //if the reward is negative, then
         //we subtract the bet money
        //then we tell the user their new balance
        cout<<"your balance was "<<tot<<endl;
        cout<<"you have lost, so your bet is subtracted"<<endl;
        tot = (tot-betMoolah);
        cout<<"your new balance is: "<<tot<<endl;
     }//end if

     //if the reward is above 0, then we add the reward to the total
     else if(reward > 0)
     {
        cout<<"your original balance was "<<tot<<endl;
        cout<<"your winnings are: "<< reward<<endl;
        tot = reward + tot;
        cout<<"your new balance is: "<<tot<<endl;
    }//end else
    else
    {
        cout<<"you have lost no money"<<endl;
        cout<<"your balance is still " <<tot<<endl;
    }
}//end of the big if

 //if the amount of money entered is above total money
 //then the money entered isn't valid at all
else
{
    cout<<"the amount of money you've entered is not valid"<<endl;
    cout<<"please try again"<<endl;
}// end else
play.shuffleDeck();
return tot;
}//end handleBlackJack

我不能完全解释你所看到的,但我确实注意到unsigned int的自由使用。特别地,在handleBlackJack中有:

unsigned int reward;

然后

reward = play.handleReward(betMoolah);
最后

if(reward < 0 )

如果奖励可以是负的,那么它就不应该是unsigned int。正如其他人建议的那样,在调试器中逐步完成它并"跟随钱"。