Wrong output (problem in the loop)

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”.

here's my program for the delete
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
void delete_rec()
{	int success;
	char newcode[3];
	int answer; 
		
	
	fp = fopen("Products.txt","r");
	fa = fopen("temproducts.txt", "w");
				
				printf("\n\n");
			do{
				printf("\n\nProduct Code:  ");
				scanf("\n");
				gets(newcode);
				
					while (fscanf(fp, "%s%s%d", product.code, product.name, &product.q)!=EOF){	
						if(strcmp(newcode,product.code)==0)//if code is same
						{		system("cls");
								printf("\n\n\n\n\tPRODUCT CODE: %.2s", product.code);
								printf("\n\n\tPRODUCT NAME: %s", product.name);
								printf("\n\n\tQUANTITY: %d", product.q);	
								printf("\n\n\tDelete the record? [Y / N]   ");
								scanf("%c", &answer);
									if (toupper(answer)=='Y')
										{printf("\tDeleted!");
											getch();
											success = 1;}
								
								else //not yes
								{	printf("\n\nPress Enter to continue...");
									fprintf(fa,"\n\n%.2s\n%s\n%d", product.code, product.name, product.q);
									success=1;}
							}// end of if newcode the same
						}//end of while
					}while(toupper(answer)=='N');
						while (fscanf(fp, "%s%s%d", product.code, product.name, &product.q)!=EOF){	
						if(strcmp(newcode,product.code)==0)//if code is same
						{fprintf(fa,"\n\n%.2s\n%s\n%d", product.code, product.name, product.q);}		
				}
				
					fclose (fp);
					fclose (fa);
					remove("Products.txt");
					rename("temproducts.txt","Products.txt");					
					
					if (success==0)
					{
						printf("No record");
					}
getch();
}
Last edited on
Topic archived. No new replies allowed.