这段代码在VC++6.0中无法编译,请帮忙改改,顺便看看有没错误,谢了!#include <stdio.h>/* Structure definition for storing the item details. */struct item_st
{
int itemcd;
char itemnm[20];
int tsales;
int ytdsales;
};
/* Structure definition for storing the sale details. */struct sale_st
{
int itemcd;
int qtysold;
};main()
{
int choice=0; /* Loop to display the menu and accept user choice. */ while(choice!=6)
{
clrscr(); /* Display menu */ printf("\tSALES MONITORING SYSTEM");
printf("\n\n1. Add new item details");
printf("\n2. Record Sale details");
printf("\n3. Delete existing Item details");
printf("\n4. Display Sale details based on Item number");
printf("\n5. Display Report on Sales Target Achieved Items");
printf("\n6. Quit");
printf("\n\nEnter choice: "); /* Accept menu choice */ scanf("%d", &choice); /* Invoke appropriate function based on menu choice */ if(choice==1)
additems();
else if(choice==2)
addsale();
else if(choice==3)
delitem();
else if(choice==4)
qrysale();
else if(choice==5)
disprep();
else if(choice==6)
exit();
else
{ printf("\n\nYou have entered an invalid menu choice. Please reenter.");
getch();
}
}
}/* Function to add new item details to the Item.dat file. */additems()
{
FILE *fp;
struct item_st itemarr[100];
int nfile=0, i, j, readctr, isize;
char check; /* Stores the size of the structure for use in file functions. */ isize=sizeof(struct item_st); /* If the file exists, it is opened in read mode. If it does not exist, a new file is created in write mode. */ fp=fopen("item.dat", "r");
if(fp==NULL)
{
fp=fopen("item.dat", "w");
if(fp==NULL)
{
printf("\nUnable to open Item file.");
getch();
return;
}
else
/* Initialize counter that determines whether a new file was created. */
nfile=1;
} i=0; /* If the file exists, its contents are read into an array. */ if(nfile==0)
{
fflush(fp);
readctr=fread(&itemarr[i], isize, 1, fp);
while(readctr==1)
{
i++;
fflush(fp);
readctr=fread(&itemarr[i], isize, 1, fp);
} /* The item code for the new item is set by adding 1 to the last item code. */ itemarr[i].itemcd=itemarr[i-1].itemcd+1;
}
else
/* For a new file, the item code for the new item is set to 1. */ itemarr[i].itemcd=1; /* Accept new item details. */ clrscr();
printf("\tADD NEW ITEM DETAILS"); printf("\n\nItem code: %d", itemarr[i].itemcd); /* Loop that ensures the item name is not left blank. */ check='n';
while(check=='n')
{
fflush(stdin);
printf("\n\nEnter Item name (up to 20 chars.): ");
gets(itemarr[i].itemnm);
if((strlen(itemarr[i].itemnm))==0)
printf("\n\tItem name must be entered.");
else
check='y';
} fflush(stdin);
printf("\nEnter Targeted sales: ");
scanf(" %d", &itemarr[i].tsales); /* Set the year to date sales to 0. The value in this field is updated by entries to the sales file. */ itemarr[i].ytdsales=0; /* Write the contents of the array to the file. */ if(nfile==1) /* If new file, write record to file already open in write mode. */
fwrite(&itemarr[i], isize, 1, fp);
else /* If existing file, close and reopen file in write mode. */
{
fclose(fp);
fopen("item.dat","w");

/* Loop to write the array contents to the file. */ for(j=0; j<=i; j++)
fwrite(&itemarr[j], isize, 1, fp);
}
printf("\nRecord written to Item file");
getch();
fclose(fp);
}/* Function to add new item sale details to the Sales.dat file. */addsale()
{
FILE *fp1, *fp2;
struct item_st itemarr[100];
struct sale_st salesarr[100];
int i, j, isize, ssize, readctr1, readctr2, nfile=0, x, y;
char check; /* Stores the size of the structures for use in file functions. */ isize=sizeof(struct item_st);
ssize=sizeof(struct sale_st); /* If the item.dat file exists, it is opened in read mode. */

fp1=fopen("item.dat", "r");
if(fp1==NULL)
{
printf("\nUnable to open Item file.");
getch();
return;
} /* If the sales.dat file exists, it is opened in read mode. If it does not exist, a new file is created in write mode. */ fp2=fopen("sales.dat", "r");
if(fp2==NULL)
{
fp2=fopen("sales.dat", "w");
if(fp2==NULL)
{
printf("\nUnable to open Sales file.");
getch();
return;
}
else
/* Initialize counter that determines whether a new file was created. */
nfile=1;
}
/* Read the contents of item.dat into an array. */ i=0;
fflush(fp1);
readctr1=fread(&itemarr[i], isize, 1, fp1);
while(readctr1==1)
{
i++;
fflush(fp1);
readctr1=fread(&itemarr[i], isize, 1, fp1);
}
/* If the sales.dat file exists, its contents are read into an array. */ j=0;
if(nfile==0)
{
fflush(fp2);
readctr2=fread(&salesarr[j], ssize, 1, fp2);
while(readctr2==1)
{
j++;
fflush(fp2);
readctr2=fread(&salesarr[j], ssize, 1, fp2);
}
} /* Accept item sale details. */ clrscr();
printf("\tADD ITEM SALE DETAILS"); check='n'; /* Loop that ensures sale details are entered for valid item codes only. */

while(check=='n')
{
fflush(stdin);
printf("\n\nEnter Item code: ");
scanf(" %d", &salesarr[j].itemcd); for(x=0; x<i; x++)
if(salesarr[j].itemcd==itemarr[x].itemcd)
{
check='y';
break;
}
if(check=='n')
{
printf("\n\n\tInvalid Item code. Please re-enter");
getch();
}
} check='n'; /* Loop that ensures the quantity sold is greater than 0. */ while(check=='n')
{
fflush(stdin);
printf("\nEnter Quantity Sold: ");
scanf(" %d", &salesarr[j].qtysold); if(salesarr[j].qtysold<1)
{
printf("\n\n\tQuantity sold must be greater than 0. Please re-enter");
getch();
}
else
check='y';
} /* Update the year to date sales in the items array by adding the quantity sold figure. */ itemarr[x].ytdsales+=salesarr[j].qtysold; /* Close and reopen the item.dat file in the write mode to write the contents of the updated array. */ fclose(fp1);
fopen("item.dat","w"); /* Loop to write the contents of the array to the file. */
for(x=0; x<i; x++)
fwrite(&itemarr[x], isize, 1, fp1); printf("\nRecord updated in Item file");
fclose(fp1); if(nfile==1) /* If new sales.dat file, write record to file already open in write mode. */
fwrite(&salesarr[j], ssize, 1, fp2);
else /* If existing file, close and reopen file in write mode. */
{
fclose(fp2);
fp2=fopen("sales.dat","w"); /* Loop to write the contents of the array to the file. */
for(y=0; y<=j; y++)
fwrite(&salesarr[y], ssize, 1, fp2);
} printf("\nRecord written to Sales file");
getch();
fclose(fp2);
}/* Function to delete existing item and item sale details. */

解决方案 »

  1.   

    还有..............
    delitem()
    {
    FILE *fp1, *fp2;
    struct item_st itemarr[100];
    struct sale_st salesarr[100];
    int isize, ssize, readctr1, readctr2, i, j, vitem, x, y, ctr1, ctr2;
    char check; /* Stores the size of the structures for use in file functions. */ isize=sizeof(struct item_st);
    ssize=sizeof(struct sale_st); /* Open both the files in read mode. */ fp1=fopen("item.dat", "r");
    if(fp1==NULL)
    {
    printf("\nUnable to open Item file.");
    getch();
    return;
    } fp2=fopen("sales.dat", "r");
    if(fp2==NULL)
    {
    printf("\nUnable to open Sales file.");
    getch();
    return;
    } /* Copy the file contents to respective arrays. */ i=0;
    fflush(fp1);
    readctr1=fread(&itemarr[i], isize, 1, fp1);
    while(readctr1==1)
    {
    i++;
    fflush(fp1);
    readctr1=fread(&itemarr[i], isize, 1, fp1);
    } j=0;
    fflush(fp2);
    readctr2=fread(&salesarr[j], ssize, 1, fp2);
    while(readctr2==1)
    {
    j++;
    fflush(fp2);
    readctr2=fread(&salesarr[j], ssize, 1, fp2);
    } clrscr();
    printf("\tDELETE ITEM DETAILS"); check='n'; /* Loop that ensures the item code entered for deletion, exists in the item array. */ while(check=='n')
    {
    fflush(stdin);
    printf("\n\nEnter Item code: ");
    scanf(" %d", &vitem); for(x=0; x<i; x++)
    if(vitem==itemarr[x].itemcd)
    {
    check='y';
    break;
    }
    if(check=='n')
    {
    printf("\n\n\tInvalid Item code. Please re-enter");
    getch();
    }
    } /* Close the files and reopen in write mode. Copy the contents of the arrays, excluding the deleted item, to the respective files. */ fclose(fp1);
    fp1=fopen("item.dat","w");
    for(x=0, ctr1=0; x<i; x++)
    {
    if(vitem!=itemarr[x].itemcd)
    { fwrite(&itemarr[x], isize, 1, fp1);
    }
    else
    ctr1++;
    } fclose(fp2);
    fp2=fopen("sales.dat","w");
    for(y=0, ctr2=0; y<j; y++)
    {
    if(vitem!=salesarr[y].itemcd)
    { fwrite(&salesarr[y], ssize, 1, fp2);
    }
    else
    ctr2++;
    } /* Display the number of records deleted from each file. */ printf("\n\n%d record(s) deleted from Item file.", ctr1);
    printf("\n\n%d record(s) deleted from Sales file.", ctr2); getch();
    fclose(fp1);
    fclose(fp2);
    }/* Function to display the sale details for a specified item. */qrysale()
    {
    FILE *fp;
    struct sale_st vsales;
    int ssize=sizeof(struct sale_st), vitem;
    char check='n'; /* Open the file in the read mode. */ fp=fopen("sales.dat", "r");
    if(fp==NULL)
    {
    printf("\nUnable to open Sales file.");
    getch();
    return;
    }
    clrscr();
    printf("\tQUERY SALES DETAILS"); /* Accept the item code for which the sale details are to be viewed. */ fflush(stdin);
    printf("\n\nEnter Item code: ");
    scanf(" %d", &vitem); fflush(fp);
    fread(&vsales, ssize, 1, fp); /* Loop to check and display sale details for the input item code. */ while(!feof(fp))
    {
    if(vitem==vsales.itemcd)
    {
    printf("\nItem %d\tQuantity sold:%d", vsales.itemcd, vsales.qtysold);
    check='y';
    }
    fflush(fp);
    fread(&vsales, ssize, 1, fp);
    }
    if(check=='n')
    printf("\nNo sale details found for the specified item.");
    getch();
    fclose(fp);
    }/* Function to display report on items that have achieved the targeted sales figure. */disprep()
    {
    FILE *fp;
    struct item_st vitem;
    int isize=sizeof(struct item_st);
    char check='n'; /* Open the file in the read mode. */ fp=fopen("item.dat", "r");
    if(fp==NULL)
    {
    printf("\nUnable to open Item file.");
    getch();
    return;
    } clrscr();
    printf("\tREPORT ON SALES TARGET ACHIEVED ITEMS\n"); fflush(fp);
    fread(&vitem, isize, 1, fp); /* Loop to check whether the sales target has been achieved, and display the details. */ while(!feof(fp))
    {
    if(vitem.tsales<=vitem.ytdsales)
    {
    printf("\n%d\t%s\t%d\t%d", vitem.itemcd, vitem.itemnm, vitem.tsales, vitem.ytdsales);
    check='y';
    }
    fflush(fp);
    fread(&vitem, isize, 1, fp);
    }
    if(check=='n')
    printf("\nNo item has achieved the targeted sales.");
    getch();
    fclose(fp);
    }