title: Debug Assertion error for C program
body: <p>Here's a link to the picture of the error message:</p>
  
  <p><a href="http://www.flickr.com/photos/76298377@N02/6798897020/in/photostream" rel="nofollow">http://www.flickr.com/photos/76298377@N02/6798897020/in/photostream</a></p>
  
  <p>Here's the actual programming problem. It's Number 3</p>
  
  <p><a href="http://books.google.com/books?id=bSy1hBCLNl8C&amp;pg=PA335&amp;lpg=PA335&amp;dq=sales.dat+c%2B%2B&amp;source=bl&amp;ots=mmN9b4WzsN&amp;sig=miAD8-u4ly8K1Mou9ZNHv90Nscc&amp;hl=en&amp;sa=X&amp;ei=2wdQT_-4OtSCsgK-l5WyDg&amp;ved=0CDcQ6AEwAg#v=onepage&amp;q=sales.dat%20c%2B%2B&amp;f=false" rel="nofollow">http://books.google.com/books?id=bSy1hBCLNl8C&amp;pg=PA335&amp;lpg=PA335&amp;dq=sales.dat+c%2B%2B&amp;source=bl&amp;ots=mmN9b4WzsN&amp;sig=miAD8-u4ly8K1Mou9ZNHv90Nscc&amp;hl=en&amp;sa=X&amp;ei=2wdQT_-4OtSCsgK-l5WyDg&amp;ved=0CDcQ6AEwAg#v=onepage&amp;q=sales.dat%20c%2B%2B&amp;f=false</a></p>
  
  <p>and Here's the source code</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;string&gt;
  #include&lt;stdio.h&gt;
  
  using namespace std;
  
  #define numItems     8
  #define numSalesP   10
  
  // the product prices 
  float prices  [numItems] = {345.0,  853.0, 471.0, 933.0, 721.0, 663.0, 507.0, 259.00};
  
  // the product numbers
  int   prodNum [numItems] = {7,      8,     9,     10,    11,    12,    13,    14};
  
  // the salespersons IDs
  int   salesP  [numSalesP] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
  
  // the output file pointers
  FILE * filePtrs[numSalesP];
  
  // sales totals for every salespersons
  float totals  [numSalesP];
  
  //get the product index from the prodNum array
  int getProdIndex (int product) {
      int i;
      for (i=0; i&lt; numItems; i++) {
          if (prodNum[i] == product) {
              return i;
          }
      }
      return -1;
  }
  
  // get a product price from the product index
  float getProdPrice (int prodIndex) {
      return prices[prodIndex];
  }
  
  // open a salesperson output file
  void openSalesPFiles () {
      int i;
      char fileName[16];;
  
      for (i=0; i&lt;numSalesP; i++) {
          sprintf(fileName, "salespers%d.dat", i+1);
  //DEBUG         cout &lt;&lt; fileName &lt;&lt; endl;
          filePtrs[i] = fopen(fileName, "r");
      }
  }
  
  // close Salespersons files
  void closeSalesPFiles () {
      int i;
      for (i=0; i&lt;numSalesP; i++) {
          fclose(filePtrs[i]);
      }
  }
  
  // get sales person index from its ID
  int getSalesPIndex (int salesPerson) {
      int i;
      for (i=0; i&lt; numSalesP; i++) {
          if (salesP[i] == salesPerson) {
              return i;
          }
      }
      return -1;
  }
  
  
  
  int main () {
      int i;                  // generic counter
      FILE * salesFile;       // the input file with all sales
      int salesPId;           // salesperson ID
      int salesPIndex;        // salesperson index in array
      int prodId;             // product ID
      int pIndex;             // product index in array
      int qty;                // quantity
      float total;            // total for one sale
  
      // open all salespersons output files
      openSalesPFiles();
  
      // open the input file
      salesFile = fopen("sales.dat", "r");
  
      // read all record in the input file
      while (!feof(salesFile)) {
  
          fscanf(salesFile, "%d %d %d", &amp;salesPId, &amp;prodId, &amp;qty);
  //DEBUG        cout &lt;&lt; salesPId &lt;&lt; " --- " &lt;&lt; prodId &lt;&lt; " --- " &lt;&lt; qty &lt;&lt; endl;
  
          // validate sales person
          salesPIndex = getSalesPIndex (salesPId);
          if (salesPIndex &lt; 0) {
              cout &lt;&lt; "Invalid Sales person ID " &lt;&lt; salesPId &lt;&lt; endl;
              continue;
          }
  
  //DEBUG        cout &lt;&lt; "Salesperson index : " &lt;&lt; salesPIndex &lt;&lt; endl;
  
          // validate product id
          pIndex = getProdIndex (prodId);
          if (pIndex &lt; 0) {
              cout &lt;&lt; "invalid product id : " &lt;&lt; prodId &lt;&lt; endl;
              fprintf(filePtrs[salesPIndex], "Invalid Product ID %d\n", prodId);
              continue;
          } 
          else {
              // compute the sale total
              total = qty * prices[pIndex];
  //DEBUG            cout &lt;&lt; "total : " &lt;&lt; total &lt;&lt; endl;;
  
              // add it to the totals for this salesperson
              totals[salesPIndex] += (qty * prices[pIndex]);
  
              // write the sale to the salesperson file
              fprintf(filePtrs[salesPIndex], "%d %d %2.2f\n", prodId, qty, total);
          }
      }
  
      // print totals in salespersons files
      for (i=0; i&lt; numSalesP; i++) {
          fprintf(filePtrs[i], "Total Sales : %8.2f\n", totals[i]);
      }
  
      // close all files
      closeSalesPFiles();
      fclose(salesFile);
  
  
  }
  </code></pre>
  
  <p>What is wrong with the code that would make me have such an error? Thanks:S</p>