这是我的代码。主要使用结构和类,一些指针。但它不是百分之百的工作。拜托!
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
using namespace std;
typedef struct team
{
string name;
int attack;
int defence;
int points;
} team;
typedef struct group
{
string teamOne;
string teamTwo;
} group;
void teaminput(team *A);
void Fixture(team& teamA, team& teamB);
int main()
{
int NumberOfTeams;
cout << "Welcome to the World Cup Simulator" << endl;
cout << "\nPlease select the number of teams participating";
cout << "\n(please note, the required number of teams per group is 4; with";
cout << "\n the number of groups corresponding to each selection shown below)." << endl;
cout << "\n 8 (2 groups)";
cin >> NumberOfTeams;
if (NumberOfTeams == 8)
{
team A;
teaminput(&A);
team B;
teaminput(&B);
team C;
teaminput(&C);
team D;
teaminput(&D);
vector<team> groupOne;
groupOne.push_back(A);
groupOne.push_back(B);
groupOne.push_back(C);
groupOne.push_back(D);
team E;
teaminput(&E);
team F;
teaminput(&F);
team G;
teaminput(&G);
team H;
teaminput(&H);
vector<team> groupTwo;
groupTwo.push_back(E);
groupTwo.push_back(F);
groupTwo.push_back(G);
groupTwo.push_back(H);
for (auto teamA = groupOne.begin(); teamA != groupOne.end(); teamA++)
{
for (auto teamB = groupTwo.begin(); teamB != groupTwo.end(); teamB++)
{
Fixture(*teamA, *teamB);
}
}
cout << "\n" << A.name << "\n" << A.attack << "\n" << A.defence;
}
return 0;
}
void teaminput(team *A)
{
team A;
string x;
int y;
for (int i = 0; i < 4; i++)
{
cout << "\nPlease enter team " << i + 1 << " data." << endl;
cout << "\nTeam Name\t\t\t: \t";
cin >> x;
A->name = x;//TeamName
cout << "Attack Level\t\t: \t";
cin >> y;
A->attack = y; //AttackLevel
cout << "Defence Level\t\t: \t";
cin >> y;
A->defence = y;//DefenceLevel
}
}
void Fixture(team& teamA, team& teamB)
{
//string teamA, teamB;
int teamAattack, teamBattack = 0;
int teamAdefence, teamBdefence = 0;
int teamAgoals = 0, teamBgoals = 0; // previously you were only initializing teamB' variables
int teamApoints = 0, teamBpoints = 0;
if (teamA.attack - teamB.defence > 0)
teamAgoals = teamA.attack - teamB.defence;
if (teamB.attack - teamA.defence > 0)
teamBgoals = teamB.attack - teamA.defence;
if (teamAgoals > teamBgoals)
{
teamA.points = teamA.points + 3;
}
else if (teamBgoals > teamAgoals)
{
teamB.points = teamB.points + 3;
}
else if (teamAgoals == teamBgoals)
{
teamA.points = teamA.points + 1;
teamB.points = teamB.points + 1;
}
}
当我运行它时,它会询问团队数量(在这种情况下,现在只有8个是有效输入)。然后,它要求团队名称。输入后,程序崩溃。
我错过了什么?
答案 0 :(得分:0)
为什么要在team A
中重新声明teaminput()
。看起来你试图取消引用局部变量而不是传入的参数?
另外,为什么要对整个程序进行编码并仅运行它?那只是要求痛苦。这里的教训是在编写时测试每个函数,这样你就不会有300行调试地狱了。
最后,一个挑剔,一个正常的编码约定是struct
和class
具有大写名称,而函数/原语应该是camelCase(如果你有反对骆驼的东西,则为lowercase_with_underscore)。它使代码更容易阅读。