Thread: Help me debug some part in my program

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Jan 2021
    Posts
    5

    Question Help me debug some part in my program

    hello, this coding works and run perfectly. just there are some minor mistakes while searching for the contact. the program will keep on print CONTACT RECORD IS NOT FOUND multiple times until the contact list arrangement is same to the list. Can someone show me the better coding for that part so that the contact will directly appear for the name searched without having to see the contact is not found. Please!


    Code:
    #include<stdio.h>
    //#include<conio.h>
    #include<string.h>
    #include<stdlib.h>
    //#include<windows.h>
    
    
    struct person
    {
        char name[50];
        char address[50];
        char father_name[35];
        char mother_name[30];
        long int mble_no;
        char sex[8];
        char mail[100];
        char ic_no[20];
    
    
        };
    void menu();
    void got();
    void start();
    void back();
    void addrecord();
    void listrecord();
    void editrecord();
    void deleterecord();
    void searchrecord();
    int main()
    {
        system("color 2f");
        start();
        return 0;
    }
    void back()
    {
        start();
    }
    void start()
    {
        menu();
    }
    void menu()
    {
        system("cls");
    printf("\n\n\t=========================================\n");
    printf("\t**********WELCOME TO PHONEBOOK***********\n");
    printf("\t=========================================\n");
    printf("\n\n\tMAIN MENU\t\t\n");
    printf("\n\t1. Add New Contact \n\n\t2. List All Contacts \n\n\t3. Edit Existing Contact \n\n\t4. Search Contact \n\n\t5. Delete Contact \n\n\t6. Exit \n\n\n\n\tENTER NO OF CHOICE : ");
    
    
    switch(getch())
    {
        case '1': addrecord();
        break;
       case '2': listrecord();
        break;
        case '3': editrecord();
        break;
        case '4': searchrecord();
        break;
        case '5': deleterecord();
        break;
        case '6': exit(0);
        break;
        default:
                    system("cls");
                    printf("\n\n\n\tERROR: ENTER NO 1 TO 6 ONLY!");
                    printf("\n\n\tEnter any key to return to main menu. ");
                    getch();
    
    
    menu();
    }
    }
            void addrecord()
    {
            char another='Y';
            system("cls");
            FILE *f;
            struct person p;
            while((another=='Y')||(another=='y')){
            system("cls");
            f=fopen("project","ab+");
            printf("\n\t===========================\n");
            printf("\t******ADD NEW CONTACT******");
            printf("\n\t===========================\n");
            printf("\n\n\tEnter name : ");
            got(p.name);
            printf("\n\n\tEnter home address: ");
            got(p.address);
            printf("\n\n\tEnter father's name: ");
            got(p.father_name);
            printf("\n\n\tEnter mother's name : ");
            got(p.mother_name);
            printf("\n\n\tEnter phone no (+60) : ");
            scanf("%ld",&p.mble_no);
            printf("\n\tEnter gender : ");
            got(p.sex);
            printf("\n\n\tEnter e-mail address : ");
             got(p.mail);
            printf("\n\n\tEnter IC No : ");
            got(p.ic_no);
            fwrite(&p,sizeof(p),1,f);
    
    
          fflush(stdin);
            printf("\n\n\n\tCONTACT HAS BEEN SUCCESFULLY ADDED!");
            printf("\n\n\tWant to add Another Contact? ( Y : Yes / N : No ) ---> ");
    fflush(stdin);
    another=getche();
     }
     if(another!='Y'||(another!='y')){
    fclose(f);
        system("cls");
        menu();
    }
    }
    
    
    void listrecord(FILE *f){
    int count=0;
    char keyPress;
    struct person p;
    f=fopen("project","rb");
    system("cls");
    printf("\n\t=============================\n");
    printf("\t******ALL CONTACT LIST******");
    printf("\n\t=============================\n");
    while(fread(&p,sizeof(p),1,f)==1){
    count++;
    
    
    printf("\n\n\n\tName = %s\n\n\tAdress = %s\n\n\tFather's name = %s\n\n\tMother's name = %s\n\n\tPhone no (+60) = %ld\n\n\tGender = %s\n\n\tE-mail = %s\n\n\tIC No = %s",p.name,p.address,p.father_name,p.mother_name,p.mble_no,p.sex,p.mail,p.ic_no);
    printf("\n\n\t-------------------------------------------------------------------");
    }
    if(count==0){
    printf("\n\n\n\tLIST STILL EMPTY. NOTHING TO DISPLAY!");
    }
    
    
    fclose(f);
        printf("\n\n\n\t- END OF LIST -\n");
        printf("\n\tEnter any key to return to main menu. ");
    
    
    	 getch();
        system("cls");
        menu();
    }
    
    
    void searchrecord()
    {
    
    
        int count=0;
    
    
        struct person p;
    
    
    FILE *f;
    char name[100];
    f=fopen("project","rb");
    if(f==NULL)
    {
        printf("\n\n\n\tERROR IN SEARCHING!\a\a\a\a");
    }
    system("cls");
    printf("\n\t===========================");
    printf("\n\t******SEARCH CONTACT******");
    printf("\n\t===========================\n");
    printf("\n\n\tENTER CONTACT NAME TO SEARCH : ");
    scanf("%s",&name);
    while(fread(&p,sizeof(p),1,f)==1){
    if(strcmp(strupr(p.name),strupr(name))==0){
        count++;
        system("cls");
            printf("\n\n\n\tDetail Information About: %s",name);
            printf("\n\n\tName : %s\n\n\tAddress : %s\n\n\tFather's Name : %s\n\n\tMother's Name : %s\n\n\tMobile No (+60) : %ld\n\n\tGender : s\n\n\tE-mail : %s\n\n\tIC No:%s",p.name,p.address,p.father_name,p.mother_name,p.mble_no,p.sex,p.mail,p.ic_no);
    }
        else
        printf("\n\n\tERROR: CONTACT FOR NAME %s IS NOT FOUND!",name);
    }
    fclose(f);
    printf("\n\n\tEnter any key to return to main menu. ");
    
    
    	 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("\n\n\tCONTACT'S DATA NOT ADDED YET.");
    
    
    		}
    	else
    	{
    		ft=fopen("temp","wb+");
    		if(ft==NULL)
    
    
                printf("\n\n\tNO CONTACT RECORD TO DELETE!");
    		else
    
    
            {
            system("cls");
            printf("\n\t===========================");
            printf("\n\t******DELETE CONTACT******");
            printf("\n\t===========================\n");
    		printf("\n\n\tENTER CONTACT NAME TO DELETE : ");
    		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("\n\n\tCONTACT NOT FOUND!");
    		remove("temp");
    	}
    		else
    		{
    			remove("project");
    			rename("temp","project");
    			printf("\n\n\n\n\tCONTACT'S RECORD SUCCESSFULLY DELETED.");
    
    
    		}
    	}
    }
     printf("\n\n\tEnter any key to return to main menu. ");
    
    
    	 getch();
        system("cls");
    menu();
    }
    
    
    void editrecord()
    {
        char another='Y';
        int c;
        FILE *f;
        int flag=0;
        struct person p,s;
        while((another=='Y')||(another=='y')){
        system("cls");
    	char  name[50];
    	f=fopen("project","rb+");
    	if(f==NULL)
    		{
    
    
    			printf("\n\n\tCONTACT'S DATA NOT ADDED YET.");
    		}
    	else
    	{
    	    system("cls");
    		printf("\n\n\tENTER CONTACT NAME TO EDIT : ");
                got(name);
                while(fread(&p,sizeof(p),1,f)==1)
                {
                    if(strcmp(name,p.name)==0)
                    {
    
    
                        printf("\n\n\n\t=================================");
                        printf("\n\t******EDIT EXISTING CONTACT******");
                        printf("\n\t=================================\n");
                        printf("\n\tEnter name: ");
                        got(s.name);
                        printf("\n\n\tEnter address: ");
                        got(s.address);
                        printf("\n\n\tEnter father name: ");
                        got(s.father_name);
                        printf("\n\n\tEnter mother name: ");
                        got(s.mother_name);
                        printf("\n\n\tEnter phone no (+60) : ");
                        scanf("%ld",&s.mble_no);
                        printf("\n\tEnter sex: ");
                        got(s.sex);
                        printf("\n\n\tEnter e-mail: ");
                        got(s.mail);
                        printf("\n\n\tEnter IC no : ");
                        got(s.ic_no);
                        fseek(f,-sizeof(p),SEEK_CUR);
                        fwrite(&s,sizeof(p),1,f);
                        flag=1;
                        break;
    
    
    
    
    
    
                    }
                    fflush(stdin);
    
    
    
    
                }
                if(flag==1)
                {
                    printf("\n\n\n\tCONTACT RECORD SUCCESSFULLY MODIFIED!");
    
    
                }
                else
                {
                        printf("\n\n\n\tCONTACT FOR THE NAME: %s IS NOT FOUND!",name);
    
    
                }
                fclose(f);
    	}
    	 printf("\n\n\tEnter any key to return to main menu. ");
    	 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';
    
    
    }
    Last edited by tomatogrower; 01-23-2021 at 02:24 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need some help on last part of program
    By coder121 in forum C Programming
    Replies: 2
    Last Post: 01-30-2017, 07:25 PM
  2. Help with part of program
    By mytrademark in forum C Programming
    Replies: 3
    Last Post: 10-13-2011, 03:24 PM
  3. why the control goes to else part in this program
    By vapanchamukhi in forum C Programming
    Replies: 2
    Last Post: 01-13-2009, 07:09 AM
  4. Replies: 3
    Last Post: 12-17-2003, 06:02 PM
  5. How do you branch to another part of the program?
    By gcn_zelda in forum C++ Programming
    Replies: 3
    Last Post: 03-16-2003, 12:27 PM

Tags for this Thread