2017-05-08 10 views
-3

プログラムを実行しようとするたびにこのエラーが発生します。このエラーメッセージが表示されるのはなぜですか? C++

このアプリケーションでは、異常終了するようにランタイムに要求しています。 詳細については、アプリケーションのサポートチームにお問い合わせください。

{0,0} 

:それはから来:: _ 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

文字列をNULLポインタで初期化しています。デバッガはどこに表示されますか、そこからなぜあなたはおそらく作業することができます。 –

+1

他人に役立つようにタイトルを改訂してください。書かれているように、それは無用に曖昧です。このサイトのすべての質問は、「なぜこのエラーメッセージが表示されるのですか? –

答えて

2

有効ではないのbasic_string: は

'のstd :: logic_error' 何を()のインスタンスを投げた後に呼び出さTERMINATE binListのイニシャライザーのリスト0は、std::stringの正しい初期化子ではありません。代わりに{"", 0}、または{}を使用することもできます。

もう1つの考え方は、配列の先頭にダミーエントリを必要としないようにプログラムロジックを修正することです。

+1

ありがとうございました! –

関連する問題