Click here to Skip to main content
15,885,985 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Im trying to write a program to read an inventory file and create an inventory report. The report is to contain the part number, price, quantity on hand, reorder point, minimum order, and order amount. The order amount is calculated when the quantity on hand falls below the reorder point. It is calculated as the sum of the reorder point and the minimum order minus the quantity on hand.

Whenever I run the program though, all I get is
"Error reading data
End of Report"

What am I doing wrong??? Help!

"
C#
#include <stdio.h>
#include <ctype.h>
int getData (FILE *spInData,
             int *partNo, int *price,
             int *quant, int *reorder, int *minimum);
void calcAmt (int *orderAmt, int quant, int minimum, int reorder);
int writeReport (FILE *spReport, int partNo, int price, int quant, int reorder,
                 int minimum, int orderAmt);

int main (void)
{
//  Local Declarations
    int partNo;
    int price;
    int quant;
    int reorder;
    int minimum;
    int orderAmt;
    FILE* spInData;
    FILE* spReport;
//  Statements
    if (!(spInData = fopen("INVENTORYDATA.TXT", "r")))
       {
        printf("Error opening INVENTORYDATA.TXT for reading");
        return 100;
       } // if file open error
    if (!(spReport = fopen("INVENTORYREPORT.TXT", "w")))
       {
        printf("Error opening inventory report\n");
        return 102;
       }
    while (getData(spInData, &partNo, &price, &quant, &reorder, &minimum))
       {
        calcAmt (&orderAmt, quant, minimum, reorder);
        writeReport (spReport, partNo, price, quant, reorder, minimum, orderAmt);
       } // while
    fclose(spInData);
    fclose (spReport);
    printf("End of Report\n");

    return 0;
}   // main
int getData (FILE *spInData,
             int *partNo, int *price,
             int *quant, int *reorder, int *minimum)
{
    int ioResult;
    ioResult = fscanf(spInData, "%d%d%d%d%d", partNo,
                      price, quant, reorder, minimum);
    if (ioResult == EOF)
        return 0;
    else if (ioResult !=5)
    {
        printf("\aError reading data\n");
    return 0;
    }
    else
        return 1;
}
void calcAmt (int *orderAmt, int quant, int minimum, int reorder)
{
    int ioResult;
    while( ioResult != EOF)
    {
        if(quant<reorder)
        {
            *orderAmt = (reorder+minimum)/quant;
            return;
        }
        else
        {
            return;
        }
    }
}
int writeReport (FILE *spReport, int partNo, int price, int quant, int reorder,
                 int minimum, int orderAmt)
{
    printf("Part No.  Price    Quantity  Reorder Point Minimum Order\n");
    fprintf(spReport, "%04d %d %d %d %d %d\n", partNo, price, quant, reorder, minimum, orderAmt);
    return 0;
}
Posted

1 solution

First of all you should also post the input file. Without it there is now way to tell if the formatting string in fscanf( ... ) is correct. That said what you should do is set a breakpoint in the line where the error message is printed and verify the results of calling the fscanf function. Here are some things you should pay attention to:

- what exactly did the function return? (ioResult == ?). Also - it's always a good idea when searching for errors to print the error code in the error message.

- what are the values of the integers you were trying to read?

If you answer both questions you should probably be able to figure out what went wrong. If not - modify this thread to include your findings (and the input file) and we can work from there.

Good luck!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900