Thread: Need help with debugging this code

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

    Question Need help with debugging this code

    So this code runs just fine on my compiler but apparently it needs to be further "debugged". Can someone help me with this code and what exactly needs to be done?


    Code:
    
    #include<stdio.h>
    
    
    #include<conio.h>
    
    
    #include<string.h>
    
    
    #include<process.h>
    
    
    #include<stdlib.h>
    
    
    #include<dos.h>
    
    
    struct contact
    
    
    {
    
    
        long ph;
    
    
        char name[20],add[20],email[30];
    
    
    } list;
    
    
    char query[20],name[20];
    
    
    FILE *fp, *ft;
    
    
    int i,n,ch,l,found;
    
    
    int main()
    
    
    {
    
    
    main:
    
    
        system("cls");    /* ************Main menu ***********************  */
    
    
        printf("\n\t **** Contact Management System v1.0 ****");
    
    
        printf("\n\n\n\t\t\tMAIN MENU\n\t\t=====================\n\t\t[1] Add new Contacts\n\t\t[2] List of current Contacts\n\t\t[3] Locate contacts\n\t\t[4] Edit Contact\n\t\t[5] Delete Contact\n\t\t[0] Exit application\n\t\t=================\n\t\t");
    
    
        printf("Enter option:");
    
    
        scanf("%d",&ch);
    
    
        switch(ch)
    
    
        {
    
    
        case 0:
    
    
            printf("\n\n\t\tAre you sure you want to exit?");
    
    
            break;
    
    
            /* *********************Add new contacts************  */
    
    
        case 1:
    
    
            system("cls");
    
    
            fp=fopen("contact.dll","a");
    
    
            for (;;)
    
    
            {
                fflush(stdin);
    
    
                printf("To exit enter blank space in the name input\nName (Use identical):");
    
    
                scanf("%[^\n]",&list.name);
    
    
                if(stricmp(list.name,"")==0 || stricmp(list.name," ")==0)
    
    
                    break;
    
    
                fflush(stdin);
    
    
                printf("Phone:");
    
    
                scanf("%ld",&list.ph);
    
    
                fflush(stdin);
    
    
                printf("address:");
    
    
                scanf("%[^\n]",&list.add);
    
    
                fflush(stdin);
    
    
                printf("email address:");
    
    
                gets(list.email);
    
    
                printf("\n");
    
    
                fwrite(&list,sizeof(list),1,fp);
    
    
            }
    
    
            fclose(fp);
    
    
            break;
    
    
            /* *********************list of contacts*************************  */
    
    
        case 2:
    
    
            system("cls");
    
    
            printf("\n\t\t================================\n\t\t\tLIST OF CONTACTS\n\t\t================================\n\nName\t\tPhone No\t    Address\t\tE-mail ad.\n=================================================================\n\n");
    
    
            for(i=97; i<=122; i=i+1)
    
    
            {
    
    
                fp=fopen("contact.dll","r");
    
    
                fflush(stdin);
    
    
                found=0;
    
    
                while(fread(&list,sizeof(list),1,fp)==1)
    
    
                {
    
    
                    if(list.name[0]==i || list.name[0]==i-32)
    
    
                    {
    
    
                        printf("\nName\t: %s\nPhone\t: %ld\nAddress\t: %s\nEmail\t: %s\n",list.name,
    
    
                               list.ph,list.add,list.email);
    
    
                        found++;
    
    
                    }
    
    
                }
    
    
                if(found!=0)
    
    
                {
                    printf("=========================================================== [%c]-(%d)\n\n",i-32,found);
    
    
                    getch();
                }
    
    
                fclose(fp);
    
    
            }
    
    
            break;
    
    
            /* *******************search contacts**********************  */
    
    
        case 3:
    
    
            system("cls");
    
    
            do
    
    
            {
    
    
                found=0;
    
    
                printf("\n\n\t..::CONTACT SEARCH\n\t===========================\n\t..::Name of contact to search: ");
    
    
                fflush(stdin);
    
    
                scanf("%[^\n]",&query);
    
    
                l=strlen(query);
    
    
                fp=fopen("contact.dll","r");
    
    
                system("cls");
    
    
                printf("\n\n..::Search result for '%s' \n===================================================\n",query);
    
    
                while(fread(&list,sizeof(list),1,fp)==1)
    
    
                {
    
    
                    for(i=0; i<=l; i++)
    
    
                        name[i]=list.name[i];
    
    
                    name[l]='\0';
    
    
                    if(stricmp(name,query)==0)
    
    
                    {
    
    
                        printf("\n..::Name\t: %s\n..::Phone\t: %ld\n..::Address\t: %s\n..::Email\t: %s\n",list.name,list.ph,list.add,list.email);
    
    
                        found++;
    
    
                        if (found%4==0)
    
    
                        {
    
    
                            printf("..::Press any key to continue...");
    
    
                            getch();
    
    
                        }
    
    
                    }
    
    
                }
    
    
                if(found==0)
    
    
                    printf("\n..::No match found!");
    
    
                else
    
    
                    printf("\n..::%d match(s) found!",found);
    
    
                fclose(fp);
    
    
                printf("\n ..::Try again?\n\n\t[1] Yes\t\t[0] No\n\t");
    
    
                scanf("%d",&ch);
    
    
            }
            while(ch==1);
    
    
            break;
    case 4:
    
    
            system("cls");
    
    
            fp=fopen("contact.dll","r");
    
    
            ft=fopen("temp.dat","w");
    
    
            fflush(stdin);
    
    
            printf("..::Edit contact\n===============================\n\n\t..::Enter the name of contact to be edited:");
    
    
            scanf("%[^\n]",name);
    
    
            while(fread(&list,sizeof(list),1,fp)==1)
    
    
            {
    
    
                if(stricmp(name,list.name)!=0)
    
    
                    fwrite(&list,sizeof(list),1,ft);
    
    
            }
    
    
            fflush(stdin);
    
    
            printf("\n\n..::Editing '%s'\n\n",name);
    
    
            printf("..::Name(Use identical):");
    
    
            scanf("%[^\n]",&list.name);
    
    
            fflush(stdin);
    
    
            printf("..::Phone:");
    
    
            scanf("%ld",&list.ph);
    
    
            fflush(stdin);
    
    
            printf("..::address:");
    
    
            scanf("%[^\n]",&list.add);
    
    
            fflush(stdin);
    
    
            printf("..::email address:");
    
    
            gets(list.email);
    
    
            printf("\n");
    
    
            fwrite(&list,sizeof(list),1,ft);
    
    
            fclose(fp);
    
    
            fclose(ft);
    
    
            remove("contact.dll");
    
    
            rename("temp.dat","contact.dll");
    
    
            break;
    
    
            /* ********************delete contacts**********************/
    
    
        case 5:
    
    
            system("cls");
    
    
            fflush(stdin);
    
    
            printf("\n\n\t..::DELETE CONTACT\n\t==========================\n\t..::Enter the name of contact to delete:");
    
    
            scanf("%[^\n]",&name);
    
    
            fp=fopen("contact.dll","r");
    
    
            ft=fopen("temp.dat","w");
    
    
            while(fread(&list,sizeof(list),1,fp)!=0)
    
    
                if (stricmp(name,list.name)!=0)
    
    
                    fwrite(&list,sizeof(list),1,ft);
    
    
            fclose(fp);
    
    
            fclose(ft);
    
    
            remove("contact.dll");
    
    
            rename("temp.dat","contact.dll");
    
    
            break;
    
    
        default:
    
    
            printf("Invalid");
    
    
            break;
    
    
    
    
        }
    
    
        printf("\n\n\n..::Enter the Choice:\n\n\t[1] Main Menu\t\t[0] Exit\n");
    
    
        scanf("%d",&ch);
    
    
        switch (ch)
    
    
        {
    
    
        case 1:
    
    
            goto main;
    
    
        case 0:
    
    
            break;
    
    
        default:
    
    
            printf("Invalid choice");
    
    
            break;
    
    
        }
    
    
        return 0;
    
    
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    Here's a list of things to do.
    1. remove dos.h
    2. remove conio.h
    3. remove process.h
    All of those are obsolete.

    4. remove gets(), the world's most dangerous function.
    It's been removed from the standard. You have no reason at all to ever use it.

    5. remove all those fflush(stdin) calls.

    6. remove your main: label and those backward goto's.
    Use a while loop.

    7. Your main is a 500 line bloat-fest, not helped by double spacing everything.
    Each one of your cases should be a separate function, taking appropriate parameters.
    Code:
    case 1:
      add_contact();
      break;
    case 2:
      list_contact();
      break;
    8. Put all those global variables in main.
    Then pass parameters to the functions you implemented fixing point 7.
    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. Help with Debugging Code
    By husslela2 in forum C Programming
    Replies: 14
    Last Post: 03-24-2010, 02:56 PM
  3. Replies: 1
    Last Post: 03-10-2010, 11:28 AM
  4. Need help in debugging this code
    By jaggesh in forum C Programming
    Replies: 4
    Last Post: 02-09-2008, 08:47 AM
  5. debugging code
    By DeViLs_SouL in forum C++ Programming
    Replies: 3
    Last Post: 12-05-2001, 01:34 PM

Tags for this Thread