为什么我收到此错误消息? C ++

时间:2017-05-08 04:32:42

标签: c++

每次尝试运行程序时都会收到此错误。

  

此应用程序已请求Runtime以不寻常的方式终止它。   有关更多信息,请联系应用程序的支持团队。   在抛出

的实例后终止调用      

'的std :: logic_error' what():basic_string :: _ M_construct null无效

#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;

}

1 个答案:

答案 0 :(得分:2)

它来自:

{0,0}

binList的初始值设定项列表中。 0不是std::string的正确初始值设定项。您也许可以使用{"", 0},甚至{}

另一个想法可能是修改你的程序逻辑,这样你就不需要在数组的开头有一个虚拟条目。