Deleting Record

THIS IS WHAT I NEED TO DO:

If the user selected D, the program will ask the user to input product code and search for it. If the product code is found, display the record of the product and ask for confirmation to delete the record, if yes, continue with the deletion, otherwise, asks again for another product code. If the product is not found, display “record not found”.

but my codes doesn't work for the deleting record
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#include<stdio.h>
#include<conio.h>
#include<ctype.h>
#include<string.h>

void items();

struct record{
	char code[2], name[10];
	int q;	
}product;
	
	char ans, choice, erase, D[2], tc;
	char newcode[2];
	int X;
	
int main ()
{
	FILE *fp, *fa;
	fp = fopen("Products.txt", "w");
	
	do
	{
		items();
		
		fprintf(fp,"\n\n%.2s\n%s\n%d", product.code, product.name, product.q);
		
	} while (toupper(ans)=='Y');
	
	fclose(fp);

	//do{
		printf("\n\nA-ADD\tD-DELETE\tE-EDIT\tT-TRANSACT\tX-EXIT");
		printf("\n\nWhat do you want to do?  ");
		scanf(" %c", &choice);
		
	if (toupper(choice)=='A')
		{
			fp = fopen("Products.txt", "a");
			printf("\n\n");
			items();
			fprintf(fp,"\n\n%.2s \n%s \n%d", product.code, product.name, product.q);
			fclose(fp);
		} 
/*	else if (toupper(choice)=='D')
			{	int success =0;
				printf("Product Code:  ");
				scanf("\n");
				gets(newcode);
					fp = fopen("Products.txt","r");
					fa = fopen("temproducts.txt", "w");
					while (fscanf(fp, "%.2s \n%s \n%d", &product.code, &product.name, &product.q)!=EOF)
					{	
						if(strcmp(newcode,product.code)==0)//if code is same
						{
							printf("%.2s \n %s \n %d", product.code, product.name, product.q);
							printf("Delete the record? [Y / N]   ");
							scanf("%c", &erase);
								if (toupper(erase)=='Y')
									{
										printf("Deleted!");
										success = 1;
									}
								else //not yes
								{
									printf("\n\nPress Enter to continue...");
									fprintf(fp,"\n\n%.2s\n%s\n%d", product.code, product.name, product.q);
									success=1;
								}
							}
						else 
						{
							fprintf(fa,"\n\n%.2s\n%s\n%d", product.code, product.name, product.q);
								}		
						}
					}
					fclose (fp);
					
						getch();
	
}


void items()
{
		printf("Enter Product Code:	");
		scanf("\n");
		gets(product.code);
		printf("Enter Product Name:	");
		scanf("\n");
		gets(product.name);
		printf("Quantity:	");
		scanf("%d", &product.q);
		printf("Enter More? [Y/N]	");
		scanf(" %c", &ans);
		printf("\n\n");
} */
Last edited on
If the product code is 2 characters long then you need 3 chars to store it because it needs a terminating NULL byte. This affects lines 9 and 14. WIth only 2 byte for a product code the strcmp at line 54 keeps comparing memory until it hits a random NULL byte or an invalid address.

Move line 74 to line 80 and fix the indentation.

At line 78, close fa and then rename temproducts.txt to Products.txt.

what should I do with the product code? I don't undersstand what you said about the product code
Last edited on
should I make it code[3] and newcode[3]??

Yes.
thanks :)
Topic archived. No new replies allowed.