2016-04-25 102 views
-6

我知道我不是最好的,但我試圖學習。我嘗試編譯我的程序,它工作。我運行該程序,但是當我到達最後時,它說轉儲出現分段錯誤。我認爲這涉及到我想要做的排序。它在我嘗試通過竊取頁面進行排序之前就起作用了。在我做到這一點後,交換停止工作。任何想法爲什麼?有分段故障C++

//main.cpp 

#include <string> 
#include "Player.h" 
#include <fstream> 
#include <iostream> 
#include <sstream> 
#include <iomanip> 
#include <stdio.h> 
#include <stdlib.h> 

using namespace std; 

int main() 
{ 

string response; 
Player* PlaArray = new Player[5]; 
cout << "Time to make your basktball team!" << endl; 
cout << "Press 1 to go by position and 2 if you do not care!" << endl; 
cin >> response; 
ofstream outFile; 
outFile.open("Team.txt"); 
ifstream inFile; 
string pick1; 
string pick2; 
string pick3; 
string pick4; 
string pick5; 
string player1; 
string player2; 
string player3; 
string player4; 
string player5; 
string firstname; 
string lastname; 
string position; 
string team; 
string pointsPG; 
string reboundsPG; 
string assistsPG; 
string stealsPG; 
string fake; 
if (response == "1") 
{ 
    cout << "How would you like to sort your Point Guards?" << endl; 
    cout << "Press 1 if you dont have a preferance, 2 for points per game(PPG), 3 for rebounds per game(RPG)," << endl; 
    cout << "Press 4 for assists per game(APG), and 5 for steals per game(SPG)." << endl; 
    cin >> pick1; 
    if (pick1 == "1") 
    { 
     for(int x = 0; x<1; x++) 
     { 
      inFile.open("PG.txt"); 
      getline(inFile, position, ','); 
      getline(inFile, firstname, ','); 
      getline(inFile, lastname, ','); 
      getline(inFile, team, ','); 
      getline(inFile, pointsPG, ','); 
      getline(inFile, reboundsPG, ','); 
      getline(inFile, assistsPG, ','); 
      getline(inFile, stealsPG, '\n'); 
      PlaArray[x].setFirstname(firstname); 
      PlaArray[x].setLastname(lastname); 
      PlaArray[x].setPosition(position); 
      PlaArray[x].setTeam(team); 
      PlaArray[x].setPointsPG(pointsPG); 
      PlaArray[x].setReboundsPG(reboundsPG); 
      PlaArray[x].setAssistsPG(assistsPG); 
      PlaArray[x].setStealsPG(stealsPG); 
      if (outFile.is_open()) 
      { 
       player1 = PlaArray[x].Report(); 
      } 
      else 
      { 
       cout << "Unable to Open File" << endl; 
      } 

     } 
     inFile.close(); 
    } 
    cout << "How would you like to sort your Shooting Guards?" << endl; 
    cout << "Press 1 if you dont have a preferance, 2 for points per game(PPG), 3 for rebounds per game(RPG)," << endl; 
    cout << "Press 4 for assists per game(APG), and 5 for steals per game(SPG)." << endl; 
    cin >> pick2; 
    if (pick2 == "1") 
     { 
     for(int x = 0; x<1; x++) 
     { 
      inFile.open("SG.txt"); 
      getline(inFile, position, ','); 
      getline(inFile, firstname, ','); 
      getline(inFile, lastname, ','); 
      getline(inFile, team, ','); 
      getline(inFile, pointsPG, ','); 
      getline(inFile, reboundsPG, ','); 
      getline(inFile, assistsPG, ','); 
      getline(inFile, stealsPG, '\n'); 
      PlaArray[x].setFirstname(firstname); 
      PlaArray[x].setLastname(lastname); 
      PlaArray[x].setPosition(position); 
      PlaArray[x].setTeam(team); 
      PlaArray[x].setPointsPG(pointsPG); 
      PlaArray[x].setReboundsPG(reboundsPG); 
      PlaArray[x].setAssistsPG(assistsPG); 
      PlaArray[x].setStealsPG(stealsPG); 
      if (outFile.is_open()) 
       { 
        player2 = PlaArray[x].Report(); 
       } 
      else 
      { 
       cout << "Unable to Open File" << endl; 
      } 

      } 
      } 
    inFile.close(); 
    cout << "How would you like to sort your Small Fowards?" << endl; 
    cout << "Press 1 if you dont have a preferance, 2 for points per game(PPG), 3 for rebounds per game(RPG)," << endl; 
    cout << "Press 4 for assists per game(APG), and 5 for steals per game(SPG)." << endl; 
    cin >> pick3; 
    if (pick3 == "1") 
    { 
     for(int x = 0; x<1; x++) 
     { 
     inFile.open("SF.txt"); 
     getline(inFile, position, ','); 
     getline(inFile, firstname, ','); 
     getline(inFile, lastname, ','); 
     getline(inFile, team, ','); 
     getline(inFile, pointsPG, ','); 
     getline(inFile, reboundsPG, ','); 
     getline(inFile, assistsPG, ','); 
     getline(inFile, stealsPG, '\n'); 
     PlaArray[x].setFirstname(firstname); 
     PlaArray[x].setLastname(lastname); 
     PlaArray[x].setPosition(position); 
     PlaArray[x].setTeam(team); 
     PlaArray[x].setPointsPG(pointsPG); 
     PlaArray[x].setReboundsPG(reboundsPG); 
     PlaArray[x].setAssistsPG(assistsPG); 
     PlaArray[x].setStealsPG(stealsPG); 
     if (outFile.is_open()) 
     { 
      player3 = PlaArray[x].Report(); 
     } 
     else 
     { 
      cout << "Unable to Open File" << endl; 
     } 

     } 
     inFile.close(); 
    } 
    cout << "How would you like to sort your Power Fowards?" << endl; 
    cout << "Press 1 if you dont have a preferance, 2 for points per game(PPG), 3 for rebounds per game(RPG)," << endl; 
    cout << "Press 4 for assists per game(APG), and 5 for steals per game(SPG)." << endl; 
    cin >> pick4; 
    if (pick4 == "1") 
    { 
     for(int x = 0; x<1; x++) 
     { 
     inFile.open("PF.txt"); 
     getline(inFile, position, ','); 
     getline(inFile, firstname, ','); 
     getline(inFile, lastname, ','); 
     getline(inFile, team, ','); 
     getline(inFile, pointsPG, ','); 
     getline(inFile, reboundsPG, ','); 
     getline(inFile, assistsPG, ','); 
     getline(inFile, stealsPG, '\n'); 
     PlaArray[x].setFirstname(firstname); 
     PlaArray[x].setLastname(lastname); 
     PlaArray[x].setPosition(position); 
     PlaArray[x].setTeam(team); 
     PlaArray[x].setPointsPG(pointsPG); 
     PlaArray[x].setReboundsPG(reboundsPG); 
     PlaArray[x].setAssistsPG(assistsPG); 
     PlaArray[x].setStealsPG(stealsPG); 
     if (outFile.is_open()) 
     { 
      player4 = PlaArray[x].Report(); 
     } 
     else 
     { 
      cout << "Unable to Open File" << endl; 
     } 

     } 
     inFile.close(); 
    } 
    cout << "How would you like to sort your Cetners Guards?" << endl; 
    cout << "Press 1 if you dont have a preferance, 2 for points per game(PPG), 3 for rebounds per game(RPG)," << endl; 
    cout << "Press 4 for assists per game(APG), and 5 for steals per game(SPG)." << endl; 
    cin >> pick5; 
    if (pick5 == "1") 
    { 
     for(int x = 0; x<1; x++) 
     { 
      inFile.open("C.txt"); 
      getline(inFile, position, ','); 
      getline(inFile, firstname, ','); 
      getline(inFile, lastname, ','); 
      getline(inFile, team, ','); 
      getline(inFile, pointsPG, ','); 
      getline(inFile, reboundsPG, ','); 
      getline(inFile, assistsPG, ','); 
      getline(inFile, stealsPG, '\n'); 
      PlaArray[x].setFirstname(firstname); 
      PlaArray[x].setLastname(lastname); 
      PlaArray[x].setPosition(position); 
      PlaArray[x].setTeam(team); 
      PlaArray[x].setPointsPG(pointsPG); 
      PlaArray[x].setReboundsPG(reboundsPG); 
      PlaArray[x].setAssistsPG(assistsPG); 
      PlaArray[x].setStealsPG(stealsPG); 
    if (outFile.is_open()) 
    { 
     player5 = PlaArray[x].Report(); 
    } 
    else 
    { 
     cout << "Unable to Open File" << endl; 
    } 

    } 
    inFile.close(); 
    } 
if (pick5 == "5"); 
{ 
    for(int x = 0; x<10; x++) 
    { 
     inFile.open("C.txt"); 
     getline(inFile, position, ','); 
      getline(inFile, firstname, ','); 
      getline(inFile, lastname, ','); 
      getline(inFile, team, ','); 
      getline(inFile, pointsPG, ','); 
      getline(inFile, reboundsPG, ','); 
      getline(inFile, assistsPG, ','); 
      getline(inFile, stealsPG, '\n'); 
      PlaArray[x].setFirstname(firstname); 
      PlaArray[x].setLastname(lastname); 
      PlaArray[x].setPosition(position); 
      PlaArray[x].setTeam(team); 
      PlaArray[x].setPointsPG(pointsPG); 
      PlaArray[x].setReboundsPG(reboundsPG); 
      PlaArray[x].setAssistsPG(assistsPG); 
      PlaArray[x].setStealsPG(stealsPG); 
    int i; 
    int j; 
    for (i = 0; i<9; i++) 
    { 
     for (j = 0; j<9; j++) 
     { 
      if (PlaArray[j].getStealsPG().compare(PlaArray[j+1].getStealsPG()) >0) 
      { 
       swap(PlaArray[j], PlaArray[j+1]); 
      } 
     } 
    } 
    } 
     for(int x = 0; x<1; x++) 
     { 
      getline(inFile, position, ','); 
      getline(inFile, firstname, ','); 
      getline(inFile, lastname, ','); 
      getline(inFile, team, ','); 
      getline(inFile, pointsPG, ','); 
      getline(inFile, reboundsPG, ','); 
      getline(inFile, assistsPG, ','); 
      getline(inFile, stealsPG, '\n'); 
      PlaArray[x].setFirstname(firstname); 
      PlaArray[x].setLastname(lastname); 
      PlaArray[x].setPosition(position); 
      PlaArray[x].setTeam(team); 
      PlaArray[x].setPointsPG(pointsPG); 
      PlaArray[x].setReboundsPG(reboundsPG); 
      PlaArray[x].setAssistsPG(assistsPG); 
      PlaArray[x].setStealsPG(stealsPG); 
     if (outFile.is_open()) 
       { 
         player5 = PlaArray[x].Report(); 
       } 
       else 
       { 
         cout << "Unable to Open File" << endl; 
       } 
     } 
    inFile.close(); 


} 
outFile.close(); 

    cout << player1 << endl; 
    cout << player2 << endl; 
    cout << player3 << endl; 
    cout << player4 << endl; 
    cout << player5 << endl; 
} 
} 
void swap(int*i, int*j) 
{ 
    int c; 
    c = *i; 
    *i = *j; 
    *j = c; 
}    
+1

它崩潰,因爲它有可怕的格式。只是在開玩笑。認真地:正確地格式化您的代碼:P – user463035818

+0

爲什麼不讓您的'播放器'和'選擇'陣列?你重複5次相同的代碼。 – PaulMcKenzie

+0

林不知道爲什麼。我是新來的,並不完全確定我在做什麼。它工作正常,直到我試圖理清我的txt文件。它打印並正在工作。我正在嘗試這樣做,類似於我在課堂上必須做的一個項目,但是我自己去做了。我知道它可能不是最漂亮的,但在這一點上,如果我能讓它工作,我會對此感到滿意。 – DaBears5423

回答

0

我不知道它是唯一的問題,但它是一個(大!)的問題。

您分配了5 Player空間

Player* PlaArray = new Player[5]; 

,但您嘗試使用他們的10

if (pick5 == "5"); 
{ 
    for(int x = 0; x<10; x++) 
    { 
     inFile.open("C.txt"); 
     getline(inFile, position, ','); 
     getline(inFile, firstname, ','); 
     getline(inFile, lastname, ','); 
     getline(inFile, team, ','); 
     getline(inFile, pointsPG, ','); 
     getline(inFile, reboundsPG, ','); 
     getline(inFile, assistsPG, ','); 
     getline(inFile, stealsPG, '\n'); 
     PlaArray[x].setFirstname(firstname); 
     PlaArray[x].setLastname(lastname); 
     PlaArray[x].setPosition(position); 

這是不好的。

p.s .:和是的:tobi303是對的:你有一個可怕的格式。我們在開玩笑,但建議這是一個很好的建議:正確格式化您的代碼。

p.p.s:抱歉我的英文不好。

+0

謝謝你擺脫了錯誤!我應該做些什麼來改善格式?它也不會交換?任何想法如何解決這個問題? – DaBears5423

+0

爲了更好的格式,您應該使用支持自動縮進的文本編輯器。我使用'vim',但也有其他的。對於交換,刪除你的函數並使用標準交換(如果你使用C++ 98或者#include ,添加'#include ',如果你使用C++ 11或C++ 14 ) – max66