Thread: help on debugging my code please

  1. #1
    Registered User
    Join Date
    Jan 2021
    Posts
    5

    Question help on debugging my code please

    This code can be read and run just fine on my compiler but apparently it have some error in the program and needs to be further debugged. can someone help me with this code and tell what exactly needs to be done?

    Code:
    #include<stdio.h>
    #include<conio.h>
    #include<string.h>
    #include<stdlib.h>
    #include<windows.h>
    struct person
    {
        char name[35];
        char address[50];
         char father_name[35];
         char mother_name[30];
        long int mble_no;
        char sex[8];
        char mail[100];
        char citizen_no[20];
    
    
        };
    void menu();
    void got();
    void start();
    void back();
    void addrecord();
    void listrecord();
    void modifyrecord();
    void deleterecord();
    void searchrecord();
    int main()
    {
        system("color 5f");
        start();
        return 0;
    }
    void back()
    {
        start();
    }
    void start()
    {
        menu();
    }
    void menu()
    {
        system("cls");
    printf("\n\n\t\t**********WELCOME TO PHONEBOOK*************");
    printf("\n\t\t-------------------------------------------");
    printf("\n\n\t  MENU\t\t\n\n");
    printf("\t1.Add New   \n\t2.List   \n\t3.Exit  \n\t4.Modify \n\t5.Search    \n\t6.Delete    \n\n\tEnter no of choice:");
    
    
    switch(getch())
    {
        case '1':
    
    
                    addrecord();
        break;
       case '2': listrecord();
        break;
        case '3': exit(0);
        break;
        case '4': modifyrecord();
        break;
        case '5': searchrecord();
        break;
        case '6': deleterecord();
        break;
        default:
                    system("cls");
                    printf("\nEnter 1 to 6 only");
                    printf("\n Enter any key");
                    getch();
    
    
    menu();
    }
    }
            void addrecord()
    {
            system("cls");
            FILE *f;
            struct person p;
            f=fopen("project","ab+");
            printf("\n Enter name: ");
            got(p.name);
            printf("\nEnter the address: ");
            got(p.address);
            printf("\nEnter father name: ");
            got(p.father_name);
            printf("\nEnter mother name: ");
            got(p.mother_name);
            printf("\nEnter phone no.:");
            scanf("%ld",&p.mble_no);
            printf("Enter sex:");
            got(p.sex);
            printf("\nEnter e-mail:");
             got(p.mail);
            printf("\nEnter citizen no:");
            got(p.citizen_no);
            fwrite(&p,sizeof(p),1,f);
    
    
          fflush(stdin);
            printf("\nrecord saved");
    
    
    fclose(f);
    
    
        printf("\n\nEnter any key");
    
    
         getch();
        system("cls");
        menu();
    }
    void listrecord()
    {
        struct person p;
        FILE *f;
    f=fopen("project","rb");
    if(f==NULL)
    {
    printf("\nfile opening error in listing :");
    
    
    exit(1);
    }
    while(fread(&p,sizeof(p),1,f)==1)
    {
         printf("\n\n\n YOUR RECORD IS\n\n ");
            printf("\nName = %s\nAdress =%s\nFather name = %s\nMother name = %s\nMobile no = %1d\nSex = %s\nE-mail = %s\nCitizen no = %s",p.name,p.address,p.father_name,p.mother_name,p.mble_no,p.sex,p.mail,p.citizen_no);
    
    
         getch();
         system("cls");
    }
    fclose(f);
     printf("\n Enter any key");
     getch();
        system("cls");
    menu();
    }
    void searchrecord()
    {
        struct person p;
    FILE *f;
    char name[100];
    
    
    f=fopen("project","rb");
    if(f==NULL)
    {
        printf("\n error in opening\a\a\a\a");
        exit(1);
    
    
    }
    printf("\nEnter name of person to search\n");
    got(name);
    while(fread(&p,sizeof(p),1,f)==1)
    {
        if(strcmp(p.name,name)==0)
        {
            printf("\n\tDetail Information About %s",name);
            printf("\nName:%s\naddress:%s\nFather name:%s\nMother name:%s\nMobile no:%ld\nsex:%s\nE-mail:%s\nCitizen no:%s",p.name,p.address,p.father_name,p.mother_name,p.mble_no,p.sex,p.mail,p.citizen_no);
        }
            else
            printf("file not found");
    
    
    }
     fclose(f);
      printf("\n Enter any key");
    
    
         getch();
        system("cls");
    menu();
    }
    void deleterecord()
    {
        struct person p;
        FILE *f,*ft;
        int flag;
        char name[100];
        f=fopen("project","rb");
        if(f==NULL)
            {
    
    
                printf("CONTACT'S DATA NOT ADDED YET.");
    
    
            }
        else
        {
            ft=fopen("temp","wb+");
            if(ft==NULL)
    
    
                printf("file opaning error");
            else
    
    
            {
    
    
    
    
            printf("Enter CONTACT'S NAME:");
            got(name);
    
    
            fflush(stdin);
            while(fread(&p,sizeof(p),1,f)==1)
            {
                if(strcmp(p.name,name)!=0)
                    fwrite(&p,sizeof(p),1,ft);
                if(strcmp(p.name,name)==0)
                    flag=1;
            }
        fclose(f);
        fclose(ft);
        if(flag!=1)
        {
            printf("NO CONACT'S RECORD TO DELETE.");
            remove("temp.txt");
        }
            else
            {
                remove("project");
                rename("temp.txt","project");
                printf("RECORD DELETED SUCCESSFULLY.");
    
    
            }
        }
    }
     printf("\n Enter any key");
    
    
         getch();
        system("cls");
    menu();
    }
    
    
    void modifyrecord()
    {
        int c;
        FILE *f;
        int flag=0;
        struct person p,s;
        char  name[50];
        f=fopen("project","rb+");
        if(f==NULL)
            {
    
    
                printf("CONTACT'S DATA NOT ADDED YET.");
                exit(1);
    
    
    
    
            }
        else
        {
            system("cls");
            printf("\nEnter CONTACT'S NAME TO MODIFY:\n");
                got(name);
                while(fread(&p,sizeof(p),1,f)==1)
                {
                    if(strcmp(name,p.name)==0)
                    {
    
    
    
    
    
    
                        printf("\n Enter name:");
                        got(s.name);
                        printf("\nEnter the address:");
                        got(s.address);
                        printf("\nEnter father name:");
                        got(s.father_name);
                        printf("\nEnter mother name:");
                        got(s.mother_name);
                        printf("\nEnter phone no:");
                        scanf("%ld",&s.mble_no);
                        printf("\nEnter sex:");
                        got(s.sex);
                        printf("\nEnter e-mail:");
                        got(s.mail);
                        printf("\nEnter citizen no\n");
                        got(s.citizen_no);
                        fseek(f,-sizeof(p),SEEK_CUR);
                        fwrite(&s,sizeof(p),1,f);
                        flag=1;
                        break;
    
    
    
    
    
    
                    }
                    fflush(stdin);
    
    
    
    
                }
                if(flag==1)
                {
                    printf("\n your data id modified");
    
    
                }
                else
                {
                        printf(" \n data is not found");
    
    
                }
                fclose(f);
        }
         printf("\n Enter any key");
         getch();
        system("cls");
        menu();
    
    
    }
    void got(char *name)
    {
    
    
       int i=0,j;
        char c,ch;
        do
        {
            c=getch();
                    if(c!=8&&c!=13)
                    {
                        *(name+i)=c;
                            putch(c);
                            i++;
                    }
                    if(c==8)
                    {
                        if(i>0)
                        {
                            i--;
                        }
                       // printf("h");
                        system("cls");
                        for(j=0;j<i;j++)
                        {
                            ch=*(name+j);
                            putch(ch);
    
    
                        }
    
    
                    }
        }while(c!=13);
          *(name+i)='\0';
    }

  2. #2
    Registered User
    Join Date
    Sep 2020
    Posts
    167
    Have you compiled it with all the compiler warnings on?

    The '- Wall' option if using GCC

  3. #3
    Registered User
    Join Date
    Jan 2021
    Posts
    5
    yes i've done it that way. just i detected that if i add 5 contact details as an example and want to delete one/certain contact only, the program will delete all of the contacts. how to debug it ?

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,484
    1. Your indentation needs work.
    Code:
    #include<stdio.h>
    //!!#include<conio.h>
    #include<string.h>
    #include<stdlib.h>
    //!!#include<windows.h>
    
    struct person {
      char name[35];
      char address[50];
      char father_name[35];
      char mother_name[30];
      long int mble_no;
      char sex[8];
      char mail[100];
      char citizen_no[20];
    };
    
    void menu();
    void got();
    void start();
    void back();
    void addrecord();
    void listrecord();
    void modifyrecord();
    void deleterecord();
    void searchrecord();
    
    int main()
    {
      system("color 5f");
      start();
      return 0;
    }
    
    void back()
    {
      start();
    }
    
    void start()
    {
      menu();
    }
    
    void menu()
    {
      system("cls");
      printf("\n\n\t\t**********WELCOME TO PHONEBOOK*************");
      printf("\n\t\t-------------------------------------------");
      printf("\n\n\t  MENU\t\t\n\n");
      printf("\t1.Add New   \n"
             "\t2.List   \n"
             "\t3.Exit  \n"
             "\t4.Modify \n"
             "\t5.Search    \n"
             "\t6.Delete    \n\n"
             "\tEnter no of choice:");
    
      switch (getch()) {
      case '1':
        addrecord();
        break;
      case '2':
        listrecord();
        break;
      case '3':
        exit(0);
        break;
      case '4':
        modifyrecord();
        break;
      case '5':
        searchrecord();
        break;
      case '6':
        deleterecord();
        break;
      default:
        system("cls");
        printf("\nEnter 1 to 6 only");
        printf("\n Enter any key");
        getch();
        menu();
      }
    }
    
    void addrecord()
    {
      system("cls");
      FILE *f;
      struct person p;
      f = fopen("project", "ab+");
      printf("\n Enter name: ");
      got(p.name);
      printf("\nEnter the address: ");
      got(p.address);
      printf("\nEnter father name: ");
      got(p.father_name);
      printf("\nEnter mother name: ");
      got(p.mother_name);
      printf("\nEnter phone no.:");
      scanf("%ld", &p.mble_no);
      printf("Enter sex:");
      got(p.sex);
      printf("\nEnter e-mail:");
      got(p.mail);
      printf("\nEnter citizen no:");
      got(p.citizen_no);
      fwrite(&p, sizeof(p), 1, f);
    
      fflush(stdin);
      printf("\nrecord saved");
      fclose(f);
    
      printf("\n\nEnter any key");
    
      getch();
      system("cls");
      menu();
    }
    
    void listrecord()
    {
      struct person p;
      FILE *f;
      f = fopen("project", "rb");
      if (f == NULL) {
        printf("\nfile opening error in listing :");
        exit(1);
      }
      while (fread(&p, sizeof(p), 1, f) == 1) {
        printf("\n\n\n YOUR RECORD IS\n\n ");
        printf
            ("\nName = %s\nAdress =%s\nFather name = %s\nMother name = %s\nMobile no = %1d\nSex = %s\nE-mail = %s\nCitizen no = %s",
             p.name, p.address, p.father_name, p.mother_name, p.mble_no, p.sex, p.mail, p.citizen_no);
        getch();
        system("cls");
      }
      fclose(f);
      printf("\n Enter any key");
      getch();
      system("cls");
      menu();
    }
    
    void searchrecord()
    {
      struct person p;
      FILE *f;
      char name[100];
    
      f = fopen("project", "rb");
      if (f == NULL) {
        printf("\n error in opening\a\a\a\a");
        exit(1);
      }
    
      printf("\nEnter name of person to search\n");
      got(name);
      while (fread(&p, sizeof(p), 1, f) == 1) {
        if (strcmp(p.name, name) == 0) {
          printf("\n\tDetail Information About %s", name);
          printf("\nName:%s\naddress:%s\nFather name:%s\nMother name:%s\nMobile no:%ld\nsex:%s\nE-mail:%s\nCitizen no:%s",
                 p.name, p.address, p.father_name, p.mother_name, p.mble_no, p.sex, p.mail, p.citizen_no);
        } else
          printf("file not found");
      }
      fclose(f);
      printf("\n Enter any key");
    
      getch();
      system("cls");
      menu();
    }
    
    void deleterecord()
    {
      struct person p;
      FILE *f, *ft;
      int flag;
      char name[100];
      f = fopen("project", "rb");
      if (f == NULL) {
        printf("CONTACT'S DATA NOT ADDED YET.");
      } else {
        ft = fopen("temp", "wb+");
        if (ft == NULL)
          printf("file opaning error");
        else
        {
          printf("Enter CONTACT'S NAME:");
          got(name);
    
          fflush(stdin);
          while (fread(&p, sizeof(p), 1, f) == 1) {
            if (strcmp(p.name, name) != 0)
              fwrite(&p, sizeof(p), 1, ft);
            if (strcmp(p.name, name) == 0)
              flag = 1;
          }
          fclose(f);
          fclose(ft);
          if (flag != 1) {
            printf("NO CONACT'S RECORD TO DELETE.");
            remove("temp.txt");
          } else {
            remove("project");
            rename("temp.txt", "project");
            printf("RECORD DELETED SUCCESSFULLY.");
          }
        }
      }
      printf("\n Enter any key");
    
      getch();
      system("cls");
      menu();
    }
    
    void modifyrecord()
    {
      int c;
      FILE *f;
      int flag = 0;
      struct person p, s;
      char name[50];
      f = fopen("project", "rb+");
      if (f == NULL) {
        printf("CONTACT'S DATA NOT ADDED YET.");
        exit(1);
      } else {
        system("cls");
        printf("\nEnter CONTACT'S NAME TO MODIFY:\n");
        got(name);
        while (fread(&p, sizeof(p), 1, f) == 1) {
          if (strcmp(name, p.name) == 0) {
            printf("\n Enter name:");
            got(s.name);
            printf("\nEnter the address:");
            got(s.address);
            printf("\nEnter father name:");
            got(s.father_name);
            printf("\nEnter mother name:");
            got(s.mother_name);
            printf("\nEnter phone no:");
            scanf("%ld", &s.mble_no);
            printf("\nEnter sex:");
            got(s.sex);
            printf("\nEnter e-mail:");
            got(s.mail);
            printf("\nEnter citizen no\n");
            got(s.citizen_no);
            fseek(f, -sizeof(p), SEEK_CUR);
            fwrite(&s, sizeof(p), 1, f);
            flag = 1;
            break;
          }
          fflush(stdin);
        }
        if (flag == 1) {
          printf("\n your data id modified");
        } else {
          printf(" \n data is not found");
        }
        fclose(f);
      }
      printf("\n Enter any key");
      getch();
      system("cls");
      menu();
    }
    
    void got(char *name)
    {
      int i = 0, j;
      char c, ch;
      do {
        c = getch();
        if (c != 8 && c != 13) {
          *(name + i) = c;
          putch(c);
          i++;
        }
        if (c == 8) {
          if (i > 0) {
            i--;
          }
          // printf("h");
          system("cls");
          for (j = 0; j < i; j++) {
            ch = *(name + j);
            putch(ch);
          }
        }
      } while (c != 13);
      *(name + i) = '\0';
    }
    2. fflush(stdin) invokes undefined behaviour.

    3. No functions have parameters, except 'got', which is mis-declared.

    4. EVERYTHING calls menu() recursively.
    You need to return from functions that menu calls, and menu itself needs a while loop.
    Or better yet, put the while loop inside the otherwise useless start function.

    > ft = fopen("temp", "wb+");
    Mmm, ok...

    > rename("temp.txt", "project");
    This isn't the file you're looking for.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. help for debugging my code
    By mathink in forum C Programming
    Replies: 22
    Last Post: 01-09-2013, 05:06 AM
  2. debugging C code
    By MASX in forum C Programming
    Replies: 3
    Last Post: 03-14-2011, 05:45 AM
  3. Help with Debugging Code
    By husslela2 in forum C Programming
    Replies: 14
    Last Post: 03-24-2010, 02:56 PM
  4. Replies: 1
    Last Post: 03-10-2010, 11:28 AM
  5. Need help in debugging this code
    By jaggesh in forum C Programming
    Replies: 4
    Last Post: 02-09-2008, 08:47 AM

Tags for this Thread