Thread: Deleting Records from a structure

  1. #1
    Registered User
    Join Date
    Aug 2002
    Posts
    34

    Question Deleting Records from a structure

    Gday, i'm having trouble with a program that accepts user input (Student Num, Last Name, First Name and PHone Num), sorts the records, prints them out, lets the user search for a record, and now i am trying to implement a function which will let the user delete a record. Everything works fine except for the deleting function, which has one or two errors. if someone could help with the deleting function, to adjust or advise where i am going wrong it would be great. It isn't very user friendly at the moment because i'm finding it hard to get a menu system working so the user must search for a record and must delete a record,so if you can help in any of the above problems, i would really appreciate it.


    Thanks Heaps.

    Code:
    #include <stdio.h> 
    #include <stdlib.h> 
    #include <string.h> 
    #include <conio.h>
    #define MAX_STUDENTS 10 
    
    typedef struct { 
    char studentNum[20]; 
    char lastName[ 15 ]; 
    char firstName[ 10 ]; 
    char phoneNum[ 15 ]; 
    } studentData; 
    
    int read_studentRecord( studentData [] ); 
    void print_studentRecord( studentData [] ); 
    void sort_studentRecord( studentData [], int ); 
    void search_studentRecord( studentData [], int );
    int delete_studentRecord(studentData details[], int); 
    
    // ************************************************** 
    //**************************************** 
    
    int main() 
    
    { 
    int n; 
    
    studentData details [MAX_STUDENTS]; //define a structure array of size MAX_STUDENTS 
    
    n = read_studentRecord( details ); 
    
    sort_studentRecord( details, n ); 
    
    print_studentRecord( details ); 
    
    search_studentRecord( details, n ); 
    
    delete_studentRecord( details, n ); 
    
    return 0; 
    } 
    
    // ************************************************** 
    //**************************************** 
    
    int read_studentRecord ( studentData details[] ) 
    { 
    int i = 0; 
    printf( "Enter Student Number (e.g. S00054321, 0 to end input)\n" ); 
    scanf( "%s", details[i].studentNum ); 
    while ( details[i].studentNum[0] != '0' ) 
    { 
    printf (" Enter Last Name:\n?"); 
    scanf ( "%s", details[i].lastName ); 
    
    printf (" Enter First Name:\n?"); 
    scanf ( "%s", details[i].firstName ); 
    
    printf (" Enter Phone Number:\n?"); 
    scanf ( "%s", &details[i].phoneNum ); 
    
    i++; 
    
    printf ( "Enter Student Number\n?"); 
    scanf ( "%s", details[i].studentNum); 
    } 
    return i; 
    } 
    
    // ************************************************** 
    //*************************************** 
    
    void sort_studentRecord ( studentData details[], int n ) 
    { 
    
    studentData hold; 
    int i, pass; 
    
    for ( pass = 1; pass <= n - 1; pass++ )//number of passes 
    for ( i = 0; i <= n - pass -1; i++ )//single pass 
    if ( strcmp( details[i].lastName, details[i+1].lastName ) == 1) {//sort in order of last name 
    hold = details[i]; 
    details[i] = details[i+1];//swap array elements if statement is true 
    details[i+1] = hold; 
    } 
    } 
    
    
    // ************************************************** 
    //*************************************** 
    
    
    void print_studentRecord( studentData details[] ) 
    { 
    int i=0; 
    system("cls");
    
    printf("Sorted by Last Name:\n\n");
    
    if (details[0].studentNum != 0) 
    printf ("\n%-10s%-16s%-11s%10s\n\n", "Student #", "Last Name", "First Name", "Phone #"); 
    
    while ( details[i].studentNum[0] != '0' ) 
    { 
    printf("%-10s%-16s%-11s%10s\n", details[i].studentNum, details[i].lastName, details[i].firstName, details[i].phoneNum); 
    
    i++; 
    
    
    
    } 
    printf("\n\n");
    printf("Press any key to search for an entry\n");
    getch();
    } 
    
    // ************************************************** 
    //*************************************** 
    
     
    
    void search_studentRecord( studentData details[], int numOfRec ) 
    { 
    int low, high, mid; 
    char searchkey[10]; 
    int flag = 0; 
    printf("Enter a Last Name to search for.\n"); 
    scanf("%s", searchkey); 
    
    low = 0; 
    high = numOfRec; 
    
    while (low <= high) { 
    mid = (low+high)/2; 
    if (strcmp(searchkey, details[mid].lastName) < 0) 
    high = mid-1; 
    else if (strcmp(searchkey, details[mid].lastName) > 0) 
    low = mid + 1; 
    else if (strcmp(searchkey, details[mid].lastName) == 0) // found match 
    { 
    
    system("cls");
    
    printf("Student I.D: %s\nPhone Number: %s\n", details[mid].studentNum, details[mid].phoneNum); 
    flag = 1; 
    break; 
    } 
    } 
    
    if (!flag) printf("Search Key not found\n"); 
    }
    //***********************************************************************************************
    
    int delete_studentRecord(studentData details[], int NumOfRec)
    {
    int i; 
    int x;//variable to hold the value of the student number to delete
    
    int max = sizeof(details); 
    
    details[max] = 0;//Mark the last record by 0. 
    
    printf("Enter a student number of the record you would like to delete:\n");
    scanf("&s", x);
    
    
    for (i = 0; i < max; i++) if (details[i].studentNum == x) break; 
    
    if (i == max) return -1; // The record not found. 
    
    for ( ; i < max; i++) details[i] = details[i + 1]; 
    
    max--; 
    
    return 0; // Successfully deleted record. 
    }

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    One way to do this is to have a member of the struct that represents the status of it. For example, it would be called "Deleted" and would be set to 0 for not deleted, and 1 for deleted. All you display/search functions etc would need to check this value to ensure they only work with Deleted=0 structs.

    When you want to delete an struct, simply loop through the array to find the one in question, and change to Deleted=1.

    Code:
    typedef struct 
    { 
       char studentNum[20]; 
       char lastName[ 15 ]; 
       char firstName[ 10 ]; 
       char phoneNum[ 15 ]; 
       int Deleted;
    } studentData;
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    Registered User
    Join Date
    Aug 2002
    Posts
    34
    Hammer, would i be able to incorporate that theory into the existing delete function, or is it totally different?
    Because i dont entirely understand how the coding would work with your method.

  4. #4
    Registered User Dr. Bebop's Avatar
    Join Date
    Sep 2002
    Posts
    96
    It's this simple, then just check if the record member Deleted is set to DELETED and ignore it if it is wherever you need to do something with the record.
    Code:
    int delete_record( studentData details[], int item_location )
    {
            details[item_location].Deleted = DELETED;
    }
    Processing error: Stupidity detected.
    ------------------------------
    Dr. Bebop
    Windows XP Professional Ed.
    Microsoft Visual Studio 6

  5. #5
    Registered User
    Join Date
    Aug 2002
    Posts
    34

    Question

    Is there any way i would be able to use strcpy to delete a record and just overwrite the record to be deleted with the last one in the array and then resort?

    e.g. strcpy(details[recordtobedeleted] == details[MAXrecord])

    if you get what i mean?

    Thanks

  6. #6
    Green Member Cshot's Avatar
    Join Date
    Jun 2002
    Posts
    892
    Haha, deja vu. Must have been reading the same code on your other thread.

    Anyways this is how you would do it with the updated structure. No need to use strcpy...

    Code:
    int delete_index;
    int last_index;
    
    if(delete_index != MAX_STUDENTS - 1)
    {
       // replace the record to be deleted with the last record in the list
       details[delete_index] = details[last_index];
       // the record has now been deleted
       // now delete the last record in the list since we've already copied it over
       details[last_index].Deleted = 1;
    }
    // simpler to delete if the last record is the one to remove
    else
       details[delete_index].Deleted = 1;
    --last_index;
    Try not.
    Do or do not.
    There is no try.

    - Master Yoda

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 03-27-2009, 04:21 AM
  2. reading a file into a block
    By mickey0 in forum C++ Programming
    Replies: 19
    Last Post: 05-03-2008, 05:53 AM
  3. Deleting data from a structure?
    By bluebob in forum C Programming
    Replies: 7
    Last Post: 05-09-2002, 03:00 PM
  4. Deleting Data within a Structure or class
    By TankCDR in forum C++ Programming
    Replies: 1
    Last Post: 02-01-2002, 10:37 PM
  5. Serial Communications in C
    By ExDigit in forum Windows Programming
    Replies: 7
    Last Post: 01-09-2002, 10:52 AM