2015-03-25 78 views
0

這裏是我的代碼。大多數使用結構和類,一些指針。但它不是100%的工作。請welp!調試世界盃模擬器C++

#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是一個有效輸入現在)。然後,它要求隊名。輸入後,程序崩潰。

我在想什麼?

+1

你真的應該儘量減少這個[最小完整的例子](http://stackoverflow.com/help/mcve),而不僅僅是爲了我們的看法 - 它可以讓你清楚的錯誤。 – Beta 2015-03-25 03:37:47

+0

在輸入函數中有「團隊A」作爲輸入函數的局部變量,而「團隊A *」作爲輸入函數的輸入參數。該功能使用哪一個? ;) – BitTickler 2015-03-25 03:42:16

回答

0

爲什麼你在teaminput()重新申報team A。它看起來像你試圖取消引用本地變量,而不是傳入的參數?

此外,你爲什麼要編寫整個程序,現在只運行它?這只是要求痛苦。這裏的教訓是在編寫它時測試每個函數,以便最終不會得到300行調試地獄。

最後,一個普通的編碼約定是struct s和class es有資本名稱,函數/基元應該是camelCase(或者如果您有反對駱駝的東西,請使用lowercase_with_underscore)。它使代碼更容易閱讀。

+0

另外,編程時,通過新代碼進行調試並關注數據和狀態以及控制流是一種很好的做法。使用調試器是編程時的一個標準步驟,而不是僅在出現問題時才使用。如果環境對調試不利,請將其改爲更好。 – BitTickler 2015-03-25 04:15:34