帮助此CHW问题... [英] HELP With This C HW Problem...

查看:64
本文介绍了帮助此CHW问题...的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写程序以读取库存文件并创建库存报告.该报告将包含零件号,价格,现有数量,重新订购点,最小订购量和订购数量.当手头数量低于重新订购点时,将计算订购数量.计算方法为再订购点和最小订购量之和减去现有数量.

每当我运行程序时,我得到的只是
读取数据时出错
报告结束"

我究竟做错了什么???救命!

"

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!

"

#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;
}

推荐答案

首先,您还应该发布输入文件.没有它,现在可以判断fscanf(...)中的格式化字符串是否正确.就是说,您应该做的是在打印错误消息的行中设置一个断点,并验证调用fscanf函数的结果.以下是您应注意的一些事项:

-函数返回的确切信息是什么? (ioResult ==?).另外-搜索错误以在错误消息中打印错误代码时,这始终是一个好主意.

-您尝试读取的整数的值是什么?

如果您回答了这两个问题,您可能应该能够找出问题所在.如果不是,请修改此线程以包括您的发现(和输入文件),我们可以从那里开始工作.

祝你好运!
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!


这篇关于帮助此CHW问题...的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆