2012-03-02 72 views
-4

下面是错误消息的图片的链接:调试断言错误的C程序

http://www.flickr.com/photos/[email protected]/6798897020/in/photostream

这里的实际编程问题。这是3号

http://books.google.com/books?id=bSy1hBCLNl8C&pg=PA335&lpg=PA335&dq=sales.dat+c%2B%2B&source=bl&ots=mmN9b4WzsN&sig=miAD8-u4ly8K1Mou9ZNHv90Nscc&hl=en&sa=X&ei=2wdQT_-4OtSCsgK-l5WyDg&ved=0CDcQ6AEwAg#v=onepage&q=sales.dat%20c%2B%2B&f=false

和这里的源代码

#include <iostream> 
#include <string> 
#include<stdio.h> 

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< 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<numSalesP; i++) { 
     sprintf(fileName, "salespers%d.dat", i+1); 
//DEBUG   cout << fileName << endl; 
     filePtrs[i] = fopen(fileName, "r"); 
    } 
} 

// close Salespersons files 
void closeSalesPFiles() { 
    int i; 
    for (i=0; i<numSalesP; i++) { 
     fclose(filePtrs[i]); 
    } 
} 

// get sales person index from its ID 
int getSalesPIndex (int salesPerson) { 
    int i; 
    for (i=0; i< 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", &salesPId, &prodId, &qty); 
//DEBUG  cout << salesPId << " --- " << prodId << " --- " << qty << endl; 

     // validate sales person 
     salesPIndex = getSalesPIndex (salesPId); 
     if (salesPIndex < 0) { 
      cout << "Invalid Sales person ID " << salesPId << endl; 
      continue; 
     } 

//DEBUG  cout << "Salesperson index : " << salesPIndex << endl; 

     // validate product id 
     pIndex = getProdIndex (prodId); 
     if (pIndex < 0) { 
      cout << "invalid product id : " << prodId << endl; 
      fprintf(filePtrs[salesPIndex], "Invalid Product ID %d\n", prodId); 
      continue; 
     } 
     else { 
      // compute the sale total 
      total = qty * prices[pIndex]; 
//DEBUG   cout << "total : " << total << 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< numSalesP; i++) { 
     fprintf(filePtrs[i], "Total Sales : %8.2f\n", totals[i]); 
    } 

    // close all files 
    closeSalesPFiles(); 
    fclose(salesFile); 


} 

什么是错的代码,会让我有这样的错误?谢谢:S

+0

投票结束为'这个问题不适合我们的问答格式。我们期望答案通常涉及事实,参考或具体的专业知识;这个问题可能会征求意见,辩论,争论,投票或扩大讨论。请尝试使您的问题更适合该网站的未来访问者,并访问[FAQ](http://stackoverflow.com/faq) – 2012-03-02 00:19:26

+0

我同意:但我应该补充一点,如果您正在关注的那本书鼓励您类似于你需要一本新书的代码,那就是C++中的C,你错过了C++ – 111111 2012-03-02 00:21:05

+0

提供的抽象概念。你为什么不返回一些值或者有更多的参数而不是全局变量。 Theres没有理由filePtrs openSalesPFiles无法获取文件列表或返回它而不是将其用作全局文件。此外,如果您允许使用C++ 11,那么您可以编写'auto varname = func();' – 2012-03-02 00:26:29

回答

9

断言来自名为feoferr.c的文件。这表明它与feof函数有关。断言说它预计stream != NULL。断言失败,所以stream显然是一个空指针。由于feof需要一个文件流参数,因此断言消息提到的流是文件流参数是一个安全的猜测。你叫feof这样的:

// open the input file 
salesFile = fopen("sales.dat", "r"); 

// read all record in the input file 
while (!feof(salesFile)) { 

所以也许salesFile是一个空指针。如您所知,当fopen无法打开文件时可能会发生这种情况。也许该文件不存在,或者您没有读取权限。


下一次您遇到错误时,请使用您面前的工具。你有一个调试器,当程序失败时它会中断你的程序。它可以将您带到失败的线路或距离它最近的线路。这应该给你一个提示,说你的问题在fopen后不久就会出现。开始在那里调查。

在代码后面设置断点,看看在你的程序失败之前你是否接近它们。如果您没有那么深入,那么在发布问题时不要包含这些功能。不要让许多不相关的代码陷入潜在的助手中来筛选,直到解决真正的问题。

当您遇到问题时,请确保您的函数返回您期望的值。如果您不知道期望他们返回什么,请阅读文档并做一些实验。确保你了解你写的所有代码。

+0

什么抢劫说。 Callstack也非常有帮助。我在我的代码中随处可见异常和断言,我几乎没有意外的行为。只是错误,停止程序,并带我到线告诉我是什么原因造成的问题 – 2012-03-02 00:28:42

+0

感谢您的帮助 – jwill22 2012-03-13 16:16:36