[英]Member Data of Class not updating
I am trying to program a NFL season simulator in C++, and while doing so, I ran into a problem when trying to update the team records. 我正在尝试用C ++编写NFL赛季模拟器,而这样做的时候,在尝试更新团队记录时遇到了问题。
My code is as follows: 我的代码如下:
team.h team.h
class Team
{
public:
// Team Data Fields
std::string teamName; // Team's Name
int teamRating; // Team's Rating
int winOrLose; // The result of the game: 1 - Win, 0 - Loss.
int totalWins; // Total Wins
int totalLosses; // Total Losses
// Class Constructors
Team ();
Team (std::string teamName);
// Member functions definitions:
void updateTeamRating();
std::string teamRecord();
};
Team::Team()
{
teamName = "Void";
teamRating = 50;
winOrLose = 0;
totalWins = 0;
totalLosses = 0;
}
Team::Team(std::string inputTeamName)
{
teamName = inputTeamName;
teamRating = 50;
winOrLose = 0;
totalWins = 0;
totalLosses = 0;
}
void Team::updateTeamRating()
{
if (winOrLose == 1)
{
teamRating = teamRating + 5;
totalWins++;
}
else if (winOrLose == 0)
{
totalLosses++;
teamRating = teamRating - 5;
if (teamRating <= 0)
{
teamRating = 5;
}
}
}
std::string Team::teamRecord()
{
return teamName + std::string(" (") + std::to_string(totalWins) + std::string("-") +
std::to_string(totalLosses) + std::string(")");
}
div.h div.h
class Divison
{
public:
std::string divName;
// 4 Teams in a Divison
Team team1;
Team team2;
Team team3;
Team team4;
Divison();
Divison(Team one, Team two, Team three, Team four, std::string givenDivName);
void game(Team team1, Team team2);
void runRegDivSeason();
Team playoffTeam();
std::string toString();
};
Divison::Divison()
{
}
Divison::Divison(Team one, Team two, Team three, Team four, std::string givenDivName)
{
team1 = one;
team2 = two;
team3 = three;
team4 = four;
divName = givenDivName;
}
void Divison::game(Team team1, Team team2)
{
int totalRating = team1.teamRating + team2.teamRating;
//cout << totalRating << endl;
int result = (rand() % totalRating) + 1;
if (result <= team1.teamRating)
{
team1.winOrLose = 1;
team2.winOrLose = 0;
team1.updateTeamRating();
team2.updateTeamRating();
std::cout << "Winner: " + team1.teamRecord() + " Loser: " + team2.teamRecord() + ".\n" << std::endl;
}
else
{
team1.winOrLose = 0;
team2.winOrLose = 1;
team1.updateTeamRating();
team2.updateTeamRating();
std::cout << "Winner: " + team2.teamRecord() + " Loser: " + team1.teamRecord() + ".\n" << std::endl;
}
}
void Divison::runRegDivSeason()
{
for (int i = 0; i < 4; i++)
{
game(team1, team2);
game(team1, team3);
game(team1, team4);
game(team2, team3);
game(team2, team4);
game(team3, team4);
}
}
std::string Divison::toString()
{
return divName + ":\n " + team1.teamRecord() + "\n "
+ team2.teamRecord() + "\n " + team3.teamRecord() + "\n "
+ team4.teamRecord() + "\n";
}
main.cpp main.cpp中
#include <iostream>
#include <cstdlib>
#include <fstream>
#include <ctime>
#include "team.h"
#include "div.h"
int main()
{
Team Boston = Team("New England Patriots");
Team Buffalo = Team("Buffalo Bills");
Team NYJ = Team("New York Jets");
Team Miami = Team("Miami Dolphins");
Divison AFCE = Divison(Boston, Buffalo, NYJ, Miami, "AFC East");
AFCE.runRegDivSeason();
std::cout << AFCE.toString() << std::endl;
return 0;
}
When I run program, I want something like: 当我运行程序时,我想要这样的东西:
C:\myfiles\mfl>test
Winner: New England Patriots (1-0) Loser: Buffalo Bills (0-1).
Winner: New York Jets (1-0) Loser: New England Patriots (1-1).
Winner: New England Patriots (2-1) Loser: Miami Dolphins (0-1).
Winner: Buffalo Bills (1-1) Loser: New York Jets (1-1).
Winner: Miami Dolphins (1-1) Loser: Buffalo Bills (1-2).
Winner: New York Jets (2-1) Loser: Miami Dolphins (1-2).
Winner: Buffalo Bills (2-2) Loser: New England Patriots (2-2).
Winner: New York Jets (3-1) Loser: New England Patriots (2-3).
Winner: Miami Dolphins (2-2) Loser: New England Patriots (2-4).
Winner: New York Jets (4-1) Loser: Buffalo Bills (2-3).
Winner: Buffalo Bills (3-3) Loser: Miami Dolphins (2-3).
Winner: New York Jets (5-1) Loser: Miami Dolphins (2-4).
Winner: Buffalo Bills (3-3) Loser: New England Patriots (2-5).
Winner: New England Patriots (3-5) Loser: New York Jets (6-2).
Winner: Miami Dolphins (3-4) Loser: New England Patriots (3-6).
Winner: New York Jets (7-2) Loser: Buffalo Bills (3-4).
Winner: Miami Dolphins (4-4) Loser: Buffalo Bills (3-5).
Winner: New York Jets (8-2) Loser: Miami Dolphins (4-5).
Winner: New England Patriots (4-6) Loser: Buffalo Bills (3-6).
Winner: New England Patriots (5-6) Loser: New York Jets (8-3).
Winner: Miami Dolphins (5-5) Loser: New England Patriots (4-7).
Winner: Buffalo Bills (4-6) Loser: New York Jets (8-4).
Winner: Buffalo Bills (5-6) Loser: Miami Dolphins (5-6).
Winner: Miami Dolphins (6-6) Loser: New York Jets (8-5).
AFC East:
New England Patriots (4-7)
Buffalo Bills (5-6)
New York Jets (8-5)
Miami Dolphins (6-6)
C:\myfiles\mfl>
But I get: 但是我得到:
C:\myfiles\mfl>test
Winner: New England Patriots (1-0) Loser: Buffalo Bills (0-1).
Winner: New York Jets (1-0) Loser: New England Patriots (0-1).
Winner: New England Patriots (1-0) Loser: Miami Dolphins (0-1).
Winner: Buffalo Bills (1-0) Loser: New York Jets (0-1).
Winner: Miami Dolphins (1-0) Loser: Buffalo Bills (0-1).
Winner: New York Jets (1-0) Loser: Miami Dolphins (0-1).
Winner: Buffalo Bills (1-0) Loser: New England Patriots (0-1).
Winner: New York Jets (1-0) Loser: New England Patriots (0-1).
Winner: Miami Dolphins (1-0) Loser: New England Patriots (0-1).
Winner: New York Jets (1-0) Loser: Buffalo Bills (0-1).
Winner: Buffalo Bills (1-0) Loser: Miami Dolphins (0-1).
Winner: New York Jets (1-0) Loser: Miami Dolphins (0-1).
Winner: Buffalo Bills (1-0) Loser: New England Patriots (0-1).
Winner: New England Patriots (1-0) Loser: New York Jets (0-1).
Winner: Miami Dolphins (1-0) Loser: New England Patriots (0-1).
Winner: New York Jets (1-0) Loser: Buffalo Bills (0-1).
Winner: Miami Dolphins (1-0) Loser: Buffalo Bills (0-1).
Winner: New York Jets (1-0) Loser: Miami Dolphins (0-1).
Winner: New England Patriots (1-0) Loser: Buffalo Bills (0-1).
Winner: New England Patriots (1-0) Loser: New York Jets (0-1).
Winner: Miami Dolphins (1-0) Loser: New England Patriots (0-1).
Winner: Buffalo Bills (1-0) Loser: New York Jets (0-1).
Winner: Buffalo Bills (1-0) Loser: Miami Dolphins (0-1).
Winner: Miami Dolphins (1-0) Loser: New York Jets (0-1).
AFC East:
New England Patriots (0-0)
Buffalo Bills (0-0)
New York Jets (0-0)
Miami Dolphins (0-0)
C:\myfiles\mfl>
As far as I can tell, this indicates that totalWins
and totalLosses
variables of each object is reset to 0 after every game, which I cannot find the reason for. 据我所知,这表明每个游戏结束后每个对象的
totalWins
和totalLosses
变量都重置为0,我找不到原因。 All of the debugging that I have done indicates that the process goes exactly how I want it to, with the exception of the reset. 我所做的所有调试均表明该过程完全按照我希望的方式进行,除了重置之外。 I would greatly appreciate any insight on why this occurs.
我将不胜感激为什么会发生这种情况。
You should pass variables as references or pointers if you want to modify their properties. 如果要修改变量的属性,则应将变量作为引用或指针传递。 Didn't tested, but try:
未测试,但尝试:
Divison::Divison(Team& one, Team& two, Team& three, Team& four, std::string givenDivName)
Or 要么
Divison::Divison(Team* one, Team* two, Team* three, Team* four, std::string givenDivName)
and call example with pointers: 并使用指针调用示例:
Divison AFCE = Divison(&Boston, &Buffalo, &NYJ, &Miami, "AFC East");
The constructor Divison(Team one, Team two, Team three, Team four, std::string givenDivName)
creates a copy of the teams thus the original object won't be changed. 构造函数
Divison(Team one, Team two, Team three, Team four, std::string givenDivName)
创建了团队的副本,因此原始对象不会更改。 Take references as @Dimitry K. suggested. 以@Dimitry K.的建议为参考。
请在div中将“ team1,team2,team3和team4变量作为指针”,并在div的构造函数中为这些指针分配地址。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.