2012-04-26 35 views
0

我的程序是根據其鄰居擁有的資源計算資源價格。運行時錯誤:程序已停止響應

它編譯,但在運行時,它打破並說程序已停止響應。

任何幫助,非常感謝!

#include <iostream> 
#include <fstream> 

using namespace std; 

class Resource{ 
    string resName; 
    int amount; 
    int price; 
    public: 
     void set_values(string resName, int amount, int price){resName=resName; amount=amount; price=price;}; 
     string get_name(){return resName;}; 
     int get_amount(){return amount;}; 
     int get_price(){return price;}; 
}; 

class State{ 
    string stName; 
    Resource resources[5]; 
    public: 
     void set_values(string stName){ 
      stName=stName; 
      Resource r; 
      r.set_values(" ", 0, 0); 
      for (int i=0; i<5; i++){ 
       resources[i] = r; 
      } 
     }; 
     string get_name(){return stName;}; 
     void addResource(string name, int amount, int price){ 
      Resource r; 
      r.set_values(name, amount, price); 
      for (int i=0; i<5; i++){ 
       if(resources[i].get_name() == " "){ 
        resources[i] = r; 
       } 
      } 
     }; 
     Resource get_resource(string resource){ 
      for(int i=0; i<5; i++){ 
       if (resources[i].get_name() == resource){ 
        return resources[i]; 
       } 
      } 
     }; 
     bool checkForResource(string resource){ 
      for(int i=0; i<5; i++){ 
       if (resources[i].get_name() == resource){ 
        return true; 
       } 
      } 
     } 

}; 

class Area{ 
    State allStates[10][10]; 
    public: 
     void initialize(){ 
      for (int i=0; i<10; i++){ 
       for (int j=0; j<10; j++){ 
        allStates[i][j].set_values(" ");  
       } 
      } 
     }; 
     void addState(string stateName){ 
      for (int row=0; row<10; row++){ 
       for (int col=0; col<10; col++){ 
        if (allStates[row][col].get_name() == " ") 
         allStates[row][col].set_values(stateName); 
       } 
      } 
     }; 
     State get_state(string name){ 
      for (int row=0; row<10; row++){ 
       for (int col=0; col<10; col++){ 
        if (allStates[row][col].get_name() == name) 
         return allStates[row][col]; 
       } 
      } 
     }; 
     void deleteState(string name){ 
      for (int row=0; row<10; row++){ 
       for (int col=0; col<10; col++){ 
        if (allStates[row][col].get_name() == name) 
         allStates[row][col].set_values(" "); 
       } 
      } 
     }; 
     void moveRowsSouth(){ 
      for(int z=0; z<10; z++){ 
       for(int y=10; y>=1; y--){ 
        allStates[y][z] = allStates[y-1][z]; 
       } 
       allStates[0][z].set_values(" "); 
      } 
     }; 
     void moveColsEast(){ 
      for(int z=0; z<10; z++){ 
       for(int y=10; y>=1; y--){ 
        allStates[z][y] = allStates[z][y-1]; 
       } 
       allStates[z][0].set_values(" "); 
      } 
     }; 
     void addNeighbor(string s1, string s2, string direction){ 
      for (int row=0; row<10; row++){ 
       for (int col=0; col<10; col++){ 
        if (allStates[row][col].get_name() == s1) 
         if (direction == "North"){ 
          if (row-1 < 0){ 
           moveRowsSouth(); 
          } 
          else { 
           if (allStates[row-1][col].get_name() == " "){ 
            allStates[row-1][col] = get_state(s2); 
            deleteState(s2); 
           } 
           else 
            cout << "Unable to add this neighbor. North is occupied."; 
          } 
         } 
         else if (direction == "South"){ 
          if (allStates[row+1][col].get_name() == " "){ 
           allStates[row+1][col] = get_state(s2); 
           deleteState(s2); 
          } 
          else 
           cout << "Unable to add this neighbor. South is occupied."; 

         } 
         else if (direction == "East"){ 
          if (allStates[row][col+1].get_name() == " "){ 
           allStates[row][col+1] = get_state(s2); 
           deleteState(s2); 
          } 
          else 
           cout << "Unable to add this neighbor. East is occupied."; 
         } 
         else if (direction == "West"){ 
          if (col-1 < 0){ 
           moveColsEast(); 
          } 
          else { 
           if (allStates[row][col-1].get_name() == " "){ 
            allStates[row][col-1] = get_state(s2); 
            deleteState(s2); 
           } 
           else 
            cout << "Unable to add this neighbor. West is occupied."; 
          } 
         } 
       } 
      } 
     }; 
     State getNeighborN(string s1){ 
      for (int row=0; row<10; row++){ 
       for (int col=0; col<10; col++){ 
        if (allStates[row][col].get_name() == s1) 
         return allStates[row-1][col]; 
       } 
      } 
     }; 
     State getNeighborS(string s1){ 
      for (int row=0; row<10; row++){ 
       for (int col=0; col<10; col++){ 
        if (allStates[row][col].get_name() == s1) 
         return allStates[row+1][col]; 
       } 
      } 
     }; 
     State getNeighborE(string s1){ 
      for (int row=0; row<10; row++){ 
       for (int col=0; col<10; col++){ 
        if (allStates[row][col].get_name() == s1) 
         return allStates[row][col+1]; 
       } 
      } 
     }; 
     State getNeighborW(string s1){ 
      for (int row=0; row<10; row++){ 
       for (int col=0; col<10; col++){ 
        if (allStates[row][col].get_name() == s1) 
         return allStates[row][col-1]; 
       } 
      } 
     }; 
}; 

int main() 
{ 
    Area a; 
    a.initialize(); 
    ifstream infile; 
    string command; 
    infile.open ("proj2.txt", ifstream::in); 
    while(!infile.eof()){ 
     infile >> command; 
     if (command == "addState"){ 
      string stateName; 
      infile >> stateName; 
      a.addState(stateName); 
     } 
     else if (command == "addNeighbor"){ 
      string state1; 
      string state2; 
      string direction; 
      infile >> state1; 
      infile >> state2; 
      infile >> direction; 
      a.addNeighbor(state1, state2, direction); 
     } 
     else if (command == "addResource"){ 
      string state; 
      string name; 
      int amount; 
      int price; 
      infile >> state; 
      infile >> name; 
      infile >> amount; 
      infile >> price; 
      a.get_state(state).addResource(name, amount, price); 
     } 
     else if (command == "getPrice"){ 
      string state; 
      string resource; 
      int amount; 
      infile >> state; 
      infile >> resource; 
      infile >> amount; 
      State N = a.getNeighborN(state); 
      State S = a.getNeighborS(state); 
      State E = a.getNeighborE(state); 
      State W = a.getNeighborW(state); 
      int countOfNeigh = 0; 
      bool stateHas = false; 
      if (N.checkForResource(resource) == true) { 
       countOfNeigh++; 
      } 
      if (S.checkForResource(resource) == true) { 
       countOfNeigh++; 
      } 
      if (E.checkForResource(resource) == true) { 
       countOfNeigh++; 
      } 
      if (W.checkForResource(resource) == true) { 
       countOfNeigh++; 
      } 
      if (a.get_state(state).checkForResource(resource) == true) { 
       stateHas = true; 
      } 
      int price = 0; 
      if ((stateHas = false && countOfNeigh == 1) || 
       (stateHas = true && countOfNeigh == 0)){ 
       price = a.get_state(state).get_resource(resource).get_price(); 
       price = price+(price*.25); 
      } 
      else if (stateHas = true && (countOfNeigh == 2 || countOfNeigh == 3)){ 
       price = a.get_state(state).get_resource(resource).get_price(); 
      } 
      else if (stateHas = true && countOfNeigh == 4){ 
       price = a.get_state(state).get_resource(resource).get_price(); 
       price = price-(price*.1); 
      } 
      else if (stateHas = false && countOfNeigh > 1){ 
       price = a.get_state(state).get_resource(resource).get_price(); 
       price = price+(price*.1); 
      } 
      cout << "The price for " << amount << " units of " << resource << " is " << price << "."; 
     } 
    } 
} 
+0

您正在查看分段錯誤。使用調試器逐步瀏覽,直到找到它正在崩潰的位置。另外,根據你的代碼判斷,你可能已經超出了某個數組的範圍。爲了確認是這種情況,你可以使用'std :: vector'並使用'at'成員函數,在這種情況下會引發異常。 – chris 2012-04-26 20:18:15

+2

sscce.org是你的朋友。 – Griwes 2012-04-26 20:21:37

回答

0

你無法檢查文件打開是否成功 - http://www.cplusplus.com/reference/iostream/ifstream/open/

如果是的話,你的程序進一個無限循環。

也沒有其他類型的錯誤檢查。在處理從文件中讀取數據時,尤其是您希望以某種方式進行格式化的數據時,這是通向災難的捷徑。

相關問題