簡體   English   中英

在 C++ 中顯示一個初始化的結構數組

[英]Display an initialized array of structure in C++

我有大量這樣的代碼:

struct SoccerTeam
{
    string teamName;
    int totalGame;
    SoccerPlayer playerStore[PLAYERS] = { { "NONE", 0 }, { "NONE", 0 }, { "NONE", 0 }, { "NONE", 0 }, { "NONE", 0 } };
};

//Function prototypes.
void showStats(SoccerTeam team[], bool);
void getTeamInfo(SoccerTeam team[]);

//Main function.
int main()
{
    bool flag = true;
    SoccerTeam leagueTeam[TEAM] = { { "NONE", 0, "NONE"}, { "NONE", 0, "NONE"}, { "NONE", 0, "NONE"}, { "NONE", 0, "NONE"} };

    //Call the function to print out the content of the initialized array.
    showStats(leagueTeam, flag);

    //Call the function to ask the user to enter values into the array.
    getTeamInfo(leagueTeam);

    //Call the show stat function again with user's input.
    showStats(leagueTeam, flag);
}

我正在嘗試使用 function 將初始化的數組顯示到屏幕上:

//Function that displays the contents of the array to the screen.
void showStats(SoccerTeam team[], bool flag)
{
    double averageG = 0.00;

    cout << "Data After Initialization:" << endl << endl;
    
    for (int i = 0; i < TEAM; i++)
    {
        cout << "The team: " << team[i].teamName << " has played " << team[i].totalGame << " games." << endl;

        for (int j = 0; j < PLAYERS; j++)
        {
            cout << right << setw(20) << "Player: " << team[j].playerStore->playerName << " has " << team[j].playerStore->goalScore << " goals. " << endl;
            averageG = team[j].playerStore->goalScore / team[j].totalGame;
            cout << "The average goals are: " << averageG << endl;
        }
    }
}

而output只顯示第一行和第三行。 我不知道它有什么問題。 我是結構新手,但我很難理解這一點:

在此處輸入圖像描述

您的錯誤代碼清楚地表明:

0xc0000094 Integer 除零異常

來自這條線:

averageG = team[j].playerStore->goalScore / team[j].totalGame;

此外,這看起來不正確:

SoccerTeam leagueTeam[TEAM] = { { "NONE", 0, "NONE"}, { "NONE", 0, "NONE"}, { "NONE", 0, "NONE"}, { "NONE", 0, "NONE"} };

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM