Increasing variable number in C Files

Im having trouble on getting the quantity up of the variable "item.iqty". For example the current quantity is 5 and in this function, the user inputs a number and it should add to the variable "item.iqty". So if if the user inputs 2 then the current quantity should be 7 now but in my program it hasnt changed. its still 5

void addQty(FILE *fp)
{
int num, qty, r, c, n;
bool found;
char name[30];
ITEM item;
FILE *fp1;

system("cls");
printf("Add item quantity");
FLUSH;
printf("\n\nEnter Item number: ");
scanf("%d",&num);
fp = fopen("d:/Files/INVENTORY.txt","rb");
fp1 = fopen("d:/Files/RECEIPTS.txt","wb");
if ((fp = fopen("d:/Files/INVENTORY.dat","r+b")) == NULL)
{
printf("Record not found");
getch();
}
else
{
found = itemNum(fp, &item, num);
if (found)
{
fclose(fp);
fclose(fp1);
fp = fopen("d:/Files/INVENTORY.txt","rb");
fscanf(fp, "%d", &item.iqty);
fp1 = fopen("d:/Files/RECEIPTS.txt","wb");
fscanf(fp1, "%d", &item.iqty);
printf("How many %s/s will be added?", item.iname);
printf("\nEnter here: ");
scanf("%d", &qty);
FLUSH;
item.iqty += qty;
printf("Item quantity is now %d", item.iqty);

c = ftell(fp);
n = c - sizeof(ITEM);
fseek(fp, n, SEEK_SET);
fwrite(&item,sizeof(ITEM),1,fp);
fwrite(&item,sizeof(ITEM),1,fp1);
}
}
fclose(fp);
fclose(fp1);
return;
}
Can you please use the code format tag to format your code.

Do you mean to overwrite item.iqty?
1
2
3
4
5
6
7
8
9
10
fp = fopen("d:/Files/INVENTORY.txt","rb");
fscanf(fp, "%d", &item.iqty);
fp1 = fopen("d:/Files/RECEIPTS.txt","wb");
fscanf(fp1, "%d", &item.iqty);
printf("How many %s/s will be added?", item.iname);
printf("\nEnter here: ");
scanf("%d", &qty);
FLUSH;
item.iqty += qty;
printf("Item quantity is now %d", item.iqty);
yes
Why? The first read is redundant.

I think you should print the value before the add, so you can be sure what you're doint. Either that, or run in a debugger and step thru your program.
Topic archived. No new replies allowed.