下面是錯誤消息的圖片的鏈接:調試斷言錯誤的C程序
http://www.flickr.com/photos/[email protected]/6798897020/in/photostream
這裏的實際編程問題。這是3號
和這裏的源代碼
#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
投票結束爲'這個問題不適合我們的問答格式。我們期望答案通常涉及事實,參考或具體的專業知識;這個問題可能會徵求意見,辯論,爭論,投票或擴大討論。請嘗試使您的問題更適合該網站的未來訪問者,並訪問[FAQ](http://stackoverflow.com/faq) – 2012-03-02 00:19:26
我同意:但我應該補充一點,如果您正在關注的那本書鼓勵您類似於你需要一本新書的代碼,那就是C++中的C,你錯過了C++ – 111111 2012-03-02 00:21:05
提供的抽象概念。你爲什麼不返回一些值或者有更多的參數而不是全局變量。 Theres沒有理由filePtrs openSalesPFiles無法獲取文件列表或返回它而不是將其用作全局文件。此外,如果您允許使用C++ 11,那麼您可以編寫'auto varname = func();' – 2012-03-02 00:26:29