Delete records in Structure

This is a discussion on Delete records in Structure within the C Programming forums, part of the General Programming Boards category; Hi guys... i need help ... how to search and delete student records in array of structure?.. i was successful ...

  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    6

    Delete records in Structure

    Hi guys...
    i need help ... how to search and delete student records in array of structure?..
    i was successful with my search and edit.. but i don't know how to make a delete function..
    Example:

    the recorded datas are like this:

    clinton
    Manuel
    2.8
    where Clinton is family name, manuel is first name and 2.8 is QPA...
    instruction is that the user will search for a family name..
    once he search clinton and it was founded it will ask do you want to delete this? then if he say yes, then it has to delete all the 3 records of this student..
    i would appreciate any help about this... thanks guys

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Post the code you have already... (in code tags, please)
    We will need more than your rough description to be of much help.

  3. #3
    Registered User
    Join Date
    Nov 2010
    Posts
    6
    Code:
    #include<stdio.h>
    #include<conio.h>
    #include<string.h>
    #include<ctype.h>
    #define max 20
    typedef struct mk{
    
    	char lname[20];
    	char fname[20];
    	float  qpa;
    }dt;
    
    
    void menu();
    display(dt aS[max],int find);
    search(dt aS[],int find);
    seaNdel(dt aS[],int find);
    main()
    {
           dt aS[max];
           char choice;
           int find=0,i=0;
           float store;
           do{
    	clrscr();
    	menu();
    	printf("\nEnter your choice: [ ]\b\b");
    	choice=toupper(getche());
           if(choice!='A' && choice!='B' && choice!='C' && choice!='D' && choice!='E')
           {
    		printf("\nInvalid Choice!!! Choose the ffs: [A/B/C/D/E]");
    		getch();
           }
    
    
           else if(find<1 && choice!='A' && choice!='E')
           {
    	printf("\nPlease Record a Data first");
    
    	getch();
           }
    
    
           else{
    	 switch(choice)
    	 {
    	 case 'A':
    		fflush(stdin);
    		find++;
    		printf("\nPlease Enter Your Last Name: ");
    		gets(aS[i].lname);
    		printf("Please Enter your first name: ");
    		gets(aS[i].fname);
    		printf("Please Enter your QPA: ");
    		scanf("%f",&store);
    		aS[i].qpa=store;
    		i++;
    		break;
    
    	case 'B':
    		 fflush(stdin);
    		 search(aS,find);
    		 getch();
    		 break;
    	case 'C':
    		 /*seaNdel(aS,find);*/
    		 getch();
    		 break;
    	case 'D':
    		fflush(stdin);
    		display(aS,find);
    		getch();
    		break;
    	 }
    	 }
    
    	 }while(choice!='E');
    	 printf("\nProgram Terminated\nThank you for using my program");
    
    getch();
    }
    display(dt aS[],int find)
    {
    	 int i,x=20,y=10;
    	 clrscr();
    
    	 gotoxy(1,y);printf("Last Name:");
    	 gotoxy(1,y+1);printf("First Name:");
    	 gotoxy(1,y+2);printf("QPA:");
    
    	 for(i=0;i<find;i++){
    
    	 gotoxy(x,y++);puts(aS[i].lname);
    	 gotoxy(x,y++);puts(aS[i].fname);
    	 gotoxy(x,y++);printf("%.1f",aS[i].qpa);
    	 x+=10; y=10;
           }
    	return;
    }
    void menu()
    {
    	printf("[A] Add Student Record\n");
    	printf("[B] Search and Edit a record\n");
    	printf("[C] Search and delete a record\n");
    	printf("[D] Display All Records\n");
    	printf("[E] Exit from the program\n");
    
    	 return;
    }
    search(dt aS[],int find)
    {
     int i,found=0;
     float p;
     char temp={0},choice;
     printf("\nPlease Enter a LastName to search: ");
     gets(temp);
     for(i=0;i<find;i++)
     {
       if(strcmpi(aS[i].lname,temp)==0)
       {
        printf("\nThe name was found...and it is %s",aS[i].lname);
        found=1;
        break;
       }
    }
    	if(found==0){
    	printf("\nThe name was not found!!!");
    	}
    
    	else {
    	printf("\nEnter a new QPA to edit: ");
    	scanf("%f",&p);
    	aS[i].qpa=p;
    	}
    
    }
    /*seaNdel(dt aS[],int find)*/
    thats my code.. and i would appreciate any help in making the search and delete function...
    thanks anyway....

  4. #4
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,831
    Well how would you delete a record? My guess is you have to move all the following records up by one to fill its place.

  5. #5
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by nonoob View Post
    Well how would you delete a record? My guess is you have to move all the following records up by one to fill its place.
    You could do that, in fact many programmers do. Alternatively you can simply flag it as "deleted" by setting a key variable to an impossible value and then re-use the empty slots the next time you add records.

  6. #6
    Just a pushpin. bernt's Avatar
    Join Date
    May 2009
    Posts
    426
    Well how would you delete a record? My guess is you have to move all the following records up by one to fill its place.
    Or move the last item in the list to that empty spot, since order isn't important here.
    Consider this post signed

  7. #7
    Codus Conjectus spongefreddie's Avatar
    Join Date
    Sep 2010
    Location
    USA
    Posts
    75
    I was working on this very issue a little while ago, and ended up using the solution that CommonTater suggested in this post. All you'd have to do is add a key (flag) to your structure like so:
    Code:
    typedef struct mk{
    	char lname[20];
    	char fname[20];
    	float  qpa;
            int flag;
    };
    Then you can control its 'existence' by toggling the int value between, e.g., 1 and 0.

    This way you don't have to move any records, just replace 'deleted' ones.
    Steering Wheel: GCC in Code::Blocks 10.05
    Batmobile: Linux Mint 14, running Linux 3.5 and KDE 4.9.2
    Rusty Old Trailer For Hauling Games: Windows XP SP3
    Engine: AMD FX-8350 Vishera 8-core
    Dashboard: NIVIDIA GTX 570 HD

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. BST delete again, but this time I think I'm close
    By tms43 in forum C++ Programming
    Replies: 9
    Last Post: 11-05-2006, 05:24 PM
  2. API "Clean Up" Functions & delete Pointers :: Winsock
    By kuphryn in forum Windows Programming
    Replies: 2
    Last Post: 05-10-2002, 06:53 PM
  3. Serial Communications in C
    By ExDigit in forum Windows Programming
    Replies: 7
    Last Post: 01-09-2002, 09:52 AM
  4. C structure within structure problem, need help
    By Unregistered in forum C Programming
    Replies: 5
    Last Post: 11-30-2001, 04:48 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21