Can someone fix my code with the removing of the string and the searching for string because my program does not erase the string and does not search on the text file Code: #include<stdio.h> #include<math.h> #include<conio.h> #include<stdlib.h> #include<string.h> main(){ char string[16][128],temp[128],search[128], cData; int i=0,s=0,rep,sel,x,eq, lastline = 0, j=0, k=0; FILE *pFile; char *tmp; do{ do{ system("cls"); printf("String Database\n\n"); printf("1 - Store a String to the Database\n"); printf("2 - Remove a String from the Database\n"); printf("3 - View Strings in the Database\n"); printf("4 - Search a String in the Database\n"); printf("5 - Exit\n"); scanf("%d",&sel); }while(sel!=1&&sel!=2&&sel!=3&&sel!=4&&sel!=5); if(sel==1){ int j=0, s=0; if((pFile = fopen("strings.txt", "r+t")) != NULL){ //get all the strings in the file while ((cData = fgetc(pFile)) != EOF){ if(cData != '\n'){//||cData!='\r\n'||cData!='\r'||cData!=NULL){ string[s][j] = cData; j++; }else{ string[s][j]='\0'; j=0; s++; } } }else{ pFile = fopen("strings.txt", "wt"); } for(i=0; i<s; i++){ printf("%s", string[i]); } do{ system("cls"); printf("Input a string: "); scanf("%s",&string[s]); system("cls"); for(j=0; j<strlen(string[s]); j++){ fputc(string[s][j], pFile); } fputc('\n', pFile); printf("\nString %s successfully added\n", string[s]); printf("Enter another string?\n\n"); printf("1 - Yes\n"); printf("2 - No\n"); scanf("%d",&rep); s++; for(i=0; i<s; i++){ printf("%s", string[i]); } }while(rep==1 && s<16); fclose(pFile); } else if(sel==2){ do{ system("cls"); printf("Select position of string to remove:\n"); int j=0, i=0; if ((pFile = fopen("strings.txt", "r+t")) != NULL){ while ((cData = fgetc(pFile)) != EOF){ if(cData != '\n'){ string[i][j] = cData; j++; }else{ string[i][j]='\0'; j=0; i++; } } } fclose(pFile); int k=0; for(k=0;k<i;k++) printf("%d - %s\n",k+1,string[k]); scanf("%d",&x); for(i=x;i<s;i++) strcpy(string[i-1],string[i]); strcpy(string[s-1],temp); do{ system("cls"); printf("String successfully deleted\n"); printf("Remove another string?\n\n"); printf("1 - Yes\n"); printf("2 - No\n"); scanf("%d",&rep); }while(rep!=1&&rep!=2); }while(rep==1); } else if(sel==3){ system("cls"); int j=0, i=0; if ((pFile = fopen("strings.txt", "r+t")) != NULL){ while ((cData = fgetc(pFile)) != EOF){ if(cData != '\n'){ string[i][j] = cData; j++; }else{ string[i][j]='\0'; j=0; i++; } } } fclose(pFile); int k=0; for(k=0;k<i;k++) printf("%d - %s\n",k+1,string[k]); printf("\nPress any key to return to main menu"); getch(); } else if(sel==4){ do{ eq=1; strcpy(search,""); system("cls"); printf("Enter string to search: "); scanf("%s",&search); for(i=0;i<s;i++){ eq=strcmp(string[i],search); printf("EQ %d",eq); if(eq==0) break; } do{ system("cls"); if(eq==0){ printf("String matched\n"); printf("Position of searched string in array is %d",i+1); } else{ printf("String not found\n"); printf("Search another string?\n\n"); printf("1 - Yes\n"); printf("2 - No\n"); scanf("%d",&rep); } }while(rep!=1&&rep!=2); }while(rep==1); } }while(sel!=5); }
What does Fu!!! mean? It sounds a lot like an abbreviation for f**k you, which is not a great way to get help from people. shabbir is right though - you should try to debug this code yourself. It's the only way you can learn. I know it's frustrating, we've all been through it and that's also how we know your only way is to stick with it. OK I've found the problem, but now you have to find it. I'm not going to tell you, because the key thing here is for YOU to learn how to debug. I will give you some hints though. The problem is in the "else if(sel==2)" block, of course. As you know. Tell me what the one-letter variables are for, and what values you think they will have at each line of code. Explain what each logical section of code does and how you think the code will work. As I said in the other thread: add some printf statements to make sure the code is really doing what you think it will. Probably as you do this, you'll find the problem yourself. So don't feel the need to explain it in full, unless after doing so you still can't see it. If at that point you still can't see it I'll try to give you a further hint.