【问题标题】:C++ mathematical issueC++数学问题
【发布时间】:2011-12-08 22:05:40
【问题描述】:

所以在我的最后一个项目中,一个使用卡片类继承的二十一点和扑克模拟器,我们必须跟踪用户的赌注和总资金。但是,在我的代码中,它做了非常奇怪的事情。例如:如果您的总金额为 1000000000 美元,您下注 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

【问题讨论】:

  • 请正确格式化您的代码。
  • 你必须单独调试你的程序。我们无法为您调试它,我们所能做的就是帮助您解决特定位置(您未确定)的特定问题(您遇到的问题)。
  • 所以它的字面意思是“你的初始余额是:1000000000,你的胜利是 200,你的新余额是 199?”我认为这是不可能的。
  • 此外,您没有接受任何问题的答案。你应该解决这个问题。
  • 请发布最低可编译代码。

标签: c++


【解决方案1】:

我无法完全解释您看到了什么,但我确实注意到unsigned int 的广泛使用。特别是,在handleBlackJack 中,您有:

unsigned int reward;

然后

reward = play.handleReward(betMoolah);

最后

if(reward < 0 )

如果奖励可以为负数,则不应是unsigned int。正如其他人建议的那样,在调试器中逐步完成它并“跟着钱走”。

【讨论】:

  • 我不敢相信我错过了。谢谢!
猜你喜欢
  • 1970-01-01
  • 2011-09-22
  • 1970-01-01
  • 2021-04-26
  • 2011-04-04
  • 1970-01-01
  • 1970-01-01
  • 2017-07-31
  • 2011-02-19
相关资源
最近更新 更多