2017-05-08 43 views
-3

我每次嘗試運行程序時都會收到此錯誤。爲什麼我會收到此錯誤訊息? C++

此應用程序已請求運行時以不尋常的方式終止它。 有關更多信息,請聯繫應用程序的支持團隊。 終止投擲

實例以後有什麼()被稱爲 '的std :: logic_error':basic_string的:: _ M_construct空無效

#include <iostream> 
#include <string> 
using namespace std; 

struct Bin 
{ 
    string desc; 
    int partsQty; 
}; 

void addParts(Bin bList[], int i); 
void removeParts(Bin bList[], int i); 
int main() { 
    char response; 
    int binNumber; 
    const int NUM_OF_BINS = 11; 
    Bin binList[NUM_OF_BINS] = { 
    {0,0}, 
    {"Valve", 10}, 
    {"Earing",5}, 
    {"Bushing",15}, 
    {"Coupling",21}, 
    {"Flange",7}, 
    {"Gear",5}, 
    {"Gear Housing",5}, 
    {"Vaccum Gripper",25}, 
    {"Cable",18}, 
    {"Rod",12} 
    }; 
for(int i=1;i < 11;i++) 
{ 
    cout << "Bin #" << i << " Part: " << binList[i].desc << " Quantity " << binList[i].partsQty << endl; 
} 
    cout << "Please select a bin or enter 0 to terminate"; 
    cin >> binNumber; 
    cout << "Would you like to add or remove parts from a certain bin?(A or R)"; 
    cin >> response; 
    if(response == 'a') 
     addParts(binList, binNumber); 
    else if(response == 'r') 
     removeParts(binList, binNumber); 
    return 0; 

} 

void addParts(Bin bList[], int i) 
{ 
    int parts; 
    int num; 
    cout << "How many parts would you like to add?"; 
    cin >> num; 
    parts = bList[i].partsQty + num; 
    cout << "Bin # " << i << " now contains " << parts << " parts"; 

} 

void removeParts(Bin bList[], int i) 
{ 
    int parts; 
    int number; 
    cout << "Which bin would you like to remove parts to?"; 
    cin >> i; 
    cout << "How many parts would you like to remove?" << endl; 
    cin >> number; 
    parts = bList[i].partsQty - number; 
    if(parts < 0) 
     cout << "Please enter a number that isn't going to make the amount of parts in the bin negative."; 
    cin >> number; 
    parts = bList[i].partsQty - number; 
    cout << "The remaining amount of parts in bin #" << i << " is " << parts; 

} 
+0

請格式化您的代碼,使其可讀。 –

+2

你正在用空指針初始化一個字符串。調試器會告訴你在哪裏,你可以從那裏找出原因。 –

+1

請修改您的標題,以便其他人使用。正如所寫,它毫無用處。幾乎在這個網站上的每個問題都可能是「爲什麼我會收到此錯誤消息?」 –

回答

2

它來源於:

{0,0} 

在您的初始化程序列表爲binList0對於std::string不是正確的初始值設定項。您也許可以使用{"", 0},或者甚至使用{}

另一個想法可能是修改您的程序邏輯,以便在陣列的開始時不需要虛設條目。

+1

非常感謝! –

相關問題