Products Inventory

The output of the quantity in my updated inventory is wrong.

Here's the code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#include<stdio.h>
#include<conio.h>

struct product{
	int Q;
	char C[5], NAME[10];
};

int main ()
{
	struct product goods[20];
	int ctr, N, UC, S, A, L=0, X=0; //S-ubtract A-dd UC- update code 
	char ans;
	
	printf("\nHow Many Products:\t");
	scanf("%d", &N);
	
	for (ctr=0; ctr<N; ctr++)
	{
	printf("\n\n%d.  Product Code:\t", ctr+1);
	scanf("\n");
	gets(goods[ctr].C);
	printf("    Product Name:\t");
	scanf("\n");
	gets(goods[ctr].NAME);
	printf("    Quantity:\t\t");
	scanf("\n%d", &goods[ctr].Q);
	} 
	
	printf("\n\nInitial Invertory of Products:");
	printf("\n\nPRODUCT CODE\t\tPRODUCT NAME\t\tQUANTITY");
	printf("\n");
	
	for (ctr=0; ctr<N; ctr++)
	{
		printf("\n%s \t\t\t %s \t\t %d", goods[ctr].C, goods[ctr].NAME, goods[ctr].Q);
	}
	
	do
	{
		
		printf("\n\n\nProduct Code:\t");
		scanf("\n");
		gets(goods[ctr].C);
		
		printf("Update Code[A-dd/S-ubtract]:\t");
		scanf(" %c", &UC);
		
		printf("Quantity:\t");
		scanf("\n%d", &X);
	
	
		if (UC == 'S')
				{ L = goods[ctr].Q - X; }
		else
				{ L = goods[ctr].Q + X;	}
	
		printf("\nUpdate More [Y/N]:\t");
		scanf(" %c", &ans);
		
	} while (ans == 'y'|| ans == 'Y');	
	 
	

	printf("\n\nUpdated Invertory of Products:");
	printf("\n\nPRODUCT CODE\t\tPRODUCT NAME\t\tQUANTITY");
	printf("\n");
	
	for (ctr=0; ctr<N; ctr++)
	{
		printf("\n%s \t\t\t %s \t\t %d", goods[ctr].C, goods[ctr].NAME, L);
	}
	
	getch();
}

Last edited on
Sample Run:

How many products: 3

Product Code: PN

Product Name: Pencil

Quantity: 50

Product Code: ER

Product Name: Eraser

Quantity: 30

Product Code: SH

Product Name: Sharpener

Quantity: 20

Initial Inventory of Products:

Product Code Product Name Quantity

PN Pencil 50

ER Eraser 30

SH Sharpener 20

Product Code: ER

Update Code: A

Quantity: 30

Update more? [Y/N] : Y

Product Code: PN

Update Code: S

Quantity: 10

Update more?[Y/N] : N

Updated Inventory of Products:

Product Code Product Name Quantity

PN Pencil 40

ER Eraser 60

SH Sharpener 20
Topic archived. No new replies allowed.