基本上我正在制作扑克游戏的骨架。我只是想让投注系统失效。我在我的代码中偶然发现的是我的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();
}
答案 0 :(得分:3)
您已将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
!