We were asked to store values from one file to another. (Reading it from a source txt file and copying it into another txt file) I got all the syntax correct but somehow, my program's got a logical error in printing in the second file (Infinite loop so that's why it won't proceed in the next while loop.) The product of this program is getting the last set of information entered only and the loop never ends.
Here's my program:
char ans;
FILE *fp,*fp2;
fp = fopen("INVENTORY.txt","w");
do{
printf("Date (mm/dd/yy): ");
scanf("%d %d %d", &inv.dt.m, &inv.dt.d, &inv.dt.yr);
printf("Part No.: ");
scanf("%d", &inv.pno);
printf("Price: ");
scanf("%f", &inv.price);
printf("Quantity On Hand: ");
scanf("%d", &inv.qty);
printf("Reorder Parts: ");
scanf("%d", &inv.rp);
printf("Monthly Order: ");
scanf("%d", &inv.mo);
if(inv.qty < inv.rp)
inv.oa = (inv.rp + inv.mo) - inv.qty;
else
inv.oa = 0;
fprintf(fp,"%d/%d/%d\t%d\t%.2f\t%d\t%d\t%d\t%.2f\n", inv.dt.m, inv.dt.d, inv.dt.yr, inv.pno, inv.price, inv.qty, inv.rp, inv.mo, inv.oa);
printf("Add another (Y/N)? ");
scanf(" %c",&ans);
printf("\n");
}while(ans == 'Y' || ans == 'y');
fclose(fp);
fp = fopen("INVENTORY.txt", "r");
fp2 = fopen("REPORT.txt", "w");
while(fscanf(fp,"%d/%d/%d\t%d\t%.2f\t%d\t%d\t%d\t%.2f\n", &inv.dt.m, &inv.dt.d, &inv.dt.yr, &inv.pno, &inv.price, &inv.qty, &inv.rp, &inv.mo, &inv.oa)!=EOF){
fprintf(fp2,"%d\t%.2f\t%d\t%d\t%d\t%.2f\n", inv.pno, inv.price, inv.qty, inv.rp, inv.mo, inv.oa);
}
fclose(fp);
fclose(fp2);
fp2 = fopen("REPORT.txt","r");
printf("Part No.\tPrice\tQuantity On Hand\tReorder Parts\tMonthly Order\tOrder Amount");
while(fscanf(fp2,"%d\t%.2f\t%d\t%d\t%d\t%.2f\n", &inv.pno, &inv.price, &inv.qty, &inv.rp, &inv.mo, &inv.oa)!=EOF){
printf("%d\t%.2f\t%d\t%d\t%d\t%.2f",inv.pno, inv.price, inv.qty, inv.rp, inv.mo, inv.oa);
}
fclose(fp2);
return 0;
I hope someone can help me with this, thank you.