2015-04-15 20 views
0

當我將它從函數中傳遞出來時,我的數組被視爲一個變量。我已經嘗試了多個修復程序,但似乎沒有任何工作。在ReadInRound函數中,文件中的所有內容都會寫入正在讀取的文件中。我想將Update []數組添加到Append函數,以便我可以追加正在寫入的文件。數組像在另一個函數中的變量一樣被處理

struct MasterFile{ 
string round, ID, fName, lName, league, team; 
double mins, twoPTA, twoPTM, threePTA, threePTM, fTA, fTM, perTwo, perThree, perFT, fouls, turnovers, points;}; 

struct Header{ 
string record, fileName, date, round;}; 

struct Footer{ 
string record, fileName, date, round; 
int recordCount; 
double mins;}; 

struct UpdateFile{ 
string record, action, round, ID, fName, lName, league, team; 
double mins, twoPTA, twoPTM, threePTA, threePTM, fTA, fTM, fouls, turnovers, points;}; 

int main(){ 


Header header[1]; 
Footer footer[500]; 
MasterFile Master[500]; 
UpdateFile Update[500]; 
MasterFile NewMaster[500]; 

int x = 0; // Round counter 
double roundCount = 0; 
int u = 0; // New master counter 
int w = 0; 

x = ReadInRound(Update, Master); 

ReadInMaster(); 

Append(roundCount, Master, Update, x, NewMaster); 

Out(w, NewMaster, u); 




system("Pause"); 
return 0;} 
double ReadInRound(UpdateFile Up[], MasterFile Master[]){ 

ifstream RoundFile; 
RoundFile.open("roundData.txt", ios::in); 

string recordType = "H,D,T"; 
double points = 0.0; 
Header header[1]; 
Footer footer[500]; 
UpdateFile Update[500]; 

int x = 0; 

if (RoundFile.is_open()) 
{//Header 
    getline(RoundFile, header[x].record, ','); 
    getline(RoundFile, header[x].fileName, ','); 
    getline(RoundFile, header[x].date, ','); 
    getline(RoundFile, header[x].round, '\n'); 
    x++; 
    while (!RoundFile.eof()) 
    {//Player Data 
     getline(RoundFile, recordType, ','); 

     if (recordType == "T") 
     {//Footer 
      //footer[x].record = recordType; 
      getline(RoundFile, footer[x].record, ','); 
      getline(RoundFile, footer[x].fileName, ','); 
      getline(RoundFile, footer[x].date, ','); 
      getline(RoundFile, footer[x].round, ','); 
      RoundFile >> Update[x].points; 
      RoundFile.ignore(100, '\n'); 
      x++; 
      break; 
     } 
     Update[x].record = recordType; 
     getline(RoundFile, Update[x].action, ','); 
     getline(RoundFile, Update[x].round, ','); 
     getline(RoundFile, Update[x].ID, ','); 
     getline(RoundFile, Update[x].fName, ','); 
     getline(RoundFile, Update[x].lName, ','); 
     getline(RoundFile, Update[x].league, ','); 
     getline(RoundFile, Update[x].team, ','); 
     RoundFile >> Update[x].mins; 
     RoundFile.ignore(100, ','); 
     RoundFile >> Update[x].twoPTA; 
     RoundFile.ignore(100, ','); 
     RoundFile >> Update[x].twoPTM; 
     RoundFile.ignore(100, ','); 
     RoundFile >> Update[x].threePTA; 
     RoundFile.ignore(100, ','); 
     RoundFile >> Update[x].threePTM; 
     RoundFile.ignore(100, ','); 
     RoundFile >> Update[x].fTA; 
     RoundFile.ignore(100, ','); 
     RoundFile >> Update[x].fTM; 
     RoundFile.ignore(100, ','); 
     RoundFile >> Update[x].fouls; 
     RoundFile.ignore(100, ','); 
     RoundFile >> Update[x].turnovers; 
     RoundFile.ignore(100, '\n'); 
     points = (Update[x].twoPTM * 2) + (Update[x].threePTM * 3) + (Update[x].fTM * 1); 
     x++; 
    } 
    //Up = Update; 

} 
else 
{ 
    cout << "Round File failed to open.\n"; 
} 

return x;} 

void Append(double& roundCount, MasterFile Master[], UpdateFile Update[], int& x, MasterFile NewMaster[]){ 
int w = 0; 
for (int d = 0; d < x; d++) 
{ 
    int e = 0; 
    if (Master[e].ID == Update[d].ID) 
    { 
     if (Update[d].action == "C") 
     { 
      NewMaster[w].ID = Update[d].ID; 
      NewMaster[w].fName = Update[d].fName; 
      NewMaster[w].lName = Update[d].lName; 
      NewMaster[w].league = Update[d].league; 
      NewMaster[w].team = Update[d].team; 
      NewMaster[w].mins = Master[e].mins + Update[d].mins; 
      NewMaster[w].twoPTA = Master[e].twoPTA + Update[d].twoPTA; 
      NewMaster[w].twoPTM = Master[e].twoPTM + Update[d].twoPTM; 
      NewMaster[w].threePTA = Master[e].threePTA + Update[d].threePTA; 
      NewMaster[w].threePTM = Master[e].threePTM + Update[d].threePTM; 
      NewMaster[w].fTA = Master[e].fTA + Update[d].fTA; 
      NewMaster[w].fTM = Master[e].fTM + Update[d].fTM; 
      NewMaster[w].perTwo = ((Master[e].perTwo) + (Update[d].twoPTM/Update[d].twoPTA)*100)/2; 
      NewMaster[w].perThree = ((Master[e].perThree) + (Update[d].threePTM/Update[d].threePTA)*100)/2; 
      NewMaster[w].perFT = ((Master[e].perFT) + (Update[d].fTM/Update[d].fTA))/2; 
      NewMaster[w].fouls = Master[e].fouls + Update[d].fouls; 
      NewMaster[w].turnovers = Master[e].turnovers + Update[d].turnovers; 
      NewMaster[w].points = Master[e].points + Update[d].points; 
      w++; 
      e++; 
     } 
     else if (Update[d].action == "D") 
     { 
      e++; 
     } 
    } 
    if (Update[d].action == "A") 
    { 
     NewMaster[w].ID = Update[d].ID; 
     NewMaster[w].fName = Update[d].fName; 
     NewMaster[w].lName = Update[d].lName; 
     NewMaster[w].league = Update[d].league; 
     NewMaster[w].team = Update[d].team; 
     NewMaster[w].mins = Update[d].mins; 
     NewMaster[w].twoPTA = Update[d].twoPTA; 
     NewMaster[w].twoPTM = Update[d].twoPTM; 
     NewMaster[w].threePTA = Update[d].threePTA; 
     NewMaster[w].threePTM = Update[d].threePTM; 
     NewMaster[w].fTA = Update[d].fTA; 
     NewMaster[w].fTM = Update[d].fTM; 
     NewMaster[w].fouls = Update[d].fouls; 
     NewMaster[w].turnovers = Update[d].turnovers; 
     w++; 
    } 
} 
} 
+0

你真的應該使用'std :: vector'或'std :: array'來代替裸數組。 –

+0

所有數組*都是*變量。 – chris

+0

我應該包括結構。基本上,發生的事情是從我的調試器中填充爲數組變成變量。 –

回答

0

double ReadInRound(UpdateFile Up[], MasterFile Master[])參數被命名爲Up,但是你繼續更新本地UpdateFile Update[500];

將它們更改爲Up

+0

我做過了,我評論了一下。它應該接近ReadInRound中while循環的末尾。 –

+0

@EricJesse NO,不能以這種方式分配數組。如果'UpdateFile'的賦值操作符被正確定義,則可以按元素分配。 – timrau

+0

假設我保留它作爲更新並將其作爲一個參數(當我沒有它本地,並保持它在主要它會使它成爲一個變量,而不是一個數組),這可能會解決我的問題? –

相關問題