Below is the link to a picture of the debug error window

http://www.flickr.com/photos/76298377@N02/6798897020/in/photostream

It's a program that i'm doing and I keep getting this error here's a link to the programming problem. Its' Number 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

Why am i getting this error. I'm clueless

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

using namespace std;

const int  numItems     8
const int  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_s(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);

    
}

Recommended Answers

All 2 Replies

That has to be the worst error message I have ever seen !!! -- I mean I had no idea when I looked at it. Fortunately, I don't use visual C++ and got a this (from clang++)

test.cpp:7:12: error: default initialization of an object of const type
      'const int'
const int  numItems 8

Hopefully, that is enough to tell you the problem. But just in case it isn't: you have created a variable numItems but you have forgotten to initialize it in a c++ way. Try either this:

const int  numItems(8);      // this works [note the ; at the end] 
const int  numSalesP=10;     // this also works.

Beyond that it compiles. Obviously I don't have the file sales.dat and it core-dumps on the line while(!feof(salesFile)) because salesFile is zero and there was not test, but maybe that doesn't matter.

Hope that helps.

The assertion tells me that the stream is null. Or in other words, that it couldn't open your file.

You may want to check the file exists and the software can get access to it. If it's there and still fails, make sure it's not open in another program. If you still can't open it, specify the full path and filename to fopen.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.