0

基本上我正在生成扑克游戏的骨架。我只是试图让投注系统崩溃。我在代码中偶然发现的是我的 int 的值不断恢复到它们的原始状态(100)。我显然做错了什么,这是我的问题(我做错了什么,我能做些什么来解决它)。

class BetSys
{
public:
    int loseM;
    int bet;
    int earn;
    int money;
    int Tmoney;
    int Omoney;
    void Loop();
    void Flow();
    void Game();

};

void BetSys::Loop()
{
int Omoney = 100;
int bet = 0;
int loseM = 0;
loseM = loseM + bet;
cout << "Your start money = " << Omoney << " \n\n\n" << endl;
Game();

}

void BetSys::Game()
{
bool win;
bool lose;
int Omoney = 100;
int* PointMon = &Omoney;
int money = 0;
int Tmoney = 0;
int bet = 0;
int earn = (bet * 2) + *PointMon;
int loseM = 0;
cout << "Place your bet here!" << endl;
cin >> bet;


money = *PointMon - bet;
cout << "Your total money after bet is " << money << "\n\n";


//betP(int money)
//{
//  money - bet = money;
//}
if (bet > 10)
{
    win = true;
    if (win)
    {
        cout << "YOU WIN! \n\n" << endl;
        /*earn = (earn) + Omoney;*/
        cout << "You earned: \n" << earn;
        Tmoney = earn + (*PointMon - bet);
        cout << "\nTotal money: \n" << Tmoney;
    }
}
else if (bet <= 10)
{
    lose = true;
    if (lose)
    {
        cout << "You Lose!\n\n\n" << endl;
        int Mlose= loseM + bet;
        cout << "You lost: \n" << Mlose;
        Tmoney = loseM + (*PointMon - bet);
        cout << "\nTotal money: \n" << Tmoney;
        cout << "\n\n\n\n";
        *PointMon = Tmoney;
        //for(int i = 0, i > 20, i++)
        //{
        //  int i=10;
        //}
        Flow();

    }
}
}
void BetSys::Flow()
{
Game();
}
4

1 回答 1

3

您已在and中int Omoney声明为类成员和局部变量。你当地的人正在隐藏班级成员。BetSys::Game()BetSys::Loop()

您需要删除它们。使用初始化列表在构造函数中初始化类成员。

编辑

向您的类添加显式公共构造函数

...
void Flow();
void Game();
BetSys();

像这样定义它:

BetSys::BetSys() : Omoney(100) {
    cout << "Omoney inited to " << Omoney << endl;
}

当您创建该类的实例时Omoney inited to 100将被打印!

于 2013-11-09T20:05:26.853 回答