首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >调试世界杯模拟器C++

调试世界杯模拟器C++
EN

Stack Overflow用户
提问于 2015-03-25 11:33:12
回答 1查看 578关注 0票数 0

这是我的代码。主要使用结构和类,还有一些指针。但它并不是100%有效的。欢迎光临!

代码语言:javascript
复制
 #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是一个有效的输入)。然后,它要求输入团队名称。在一次输入之后,程序崩溃。

我遗漏了什么??

EN

回答 1

Stack Overflow用户

发布于 2015-03-25 11:44:27

为什么要在teaminput()中重新声明team A。看起来您正在尝试取消引用本地变量,而不是传入的参数?

另外,为什么你要编写整个程序,现在才运行它呢?那只是自找麻烦。这里的教训是在编写时测试每个函数,这样您就不会以300行调试地狱结束。

最后,吹毛求疵,一个正常的编码约定是,structs和classes都有大写名称,函数/原语应该是camelCase (如果你对骆驼有什么反对的话,就是lowercase_with_underscore )。这使得代码更容易阅读。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/29246956

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档