Thread: Cannot delete last element in array

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

    Question Cannot delete last element in array

    Hi, i'm wondering if someone can tell me why this deleting function will not erase the final element in the array
    I can delete all others except for the last element
    The function is in blue

    Code:
    #include <stdio.h> 
    #include <stdlib.h> 
    #include <string.h> 
    #include <conio.h>
    #define MAX_STUDENTS 10 
    
    typedef struct { 
    int recordNum;
    char studentNum[20]; 
    char lastName[ 15 ]; 
    char firstName[ 10 ]; 
    char phoneNum[ 15 ]; 
    int Deleted;
    } studentData; 
    
    void menu (void);
    int read_studentRecord( studentData [] ); 
    void print_studentRecord( studentData [] ); 
    void sort_studentRecord( studentData [], int ); 
    void search_studentRecord( studentData [], int ); 
    int delete_studentRecord(studentData [], int);
    
    // ************************************************** 
    //**************************************** 
    
    int main() 
    
    { 
    
    menu();
    
    return 0; 
    } 
    
    // ************************************************** 
    //**************************************** 
    void menu (void)
    {
       int n; 
       int choice;
       studentData details [MAX_STUDENTS]; //define a structure array of size MAX_STUDENTS
    
       printf("Press 1 to Enter Records\n\n");
       printf("Press 2 to View records in sorted form\n\n");
       printf("Press 3 to Search for a record\n\n");
       printf("Press 4 to Delete a record\n\n");
       printf("Press 0 to exit\n\n");
       scanf("%d", &choice);
    
       while (choice != 0)
       {
          if (choice == 1)
          {
    		 system("cls");
             n = read_studentRecord( details );
          }
          else if (choice == 2)
          {
             system("cls");  
             sort_studentRecord( details, n ); 
             print_studentRecord( details ); 
          }
          else if (choice == 3)
          {
             system("cls"); 
             search_studentRecord( details, n );
          }
          else if (choice == 4)
          {
    		  system("cls");
              delete_studentRecord( details, n ); 
          }
          else
          {
             printf("Invalid input\n");
          }
          printf("Press 1 to Enter Records\n\n");
          printf("Press 2 to View records in sorted form\n\n");
          printf("Press 3 to Search for a record\n\n");
          printf("Press 4 to Delete a record\n\n");
          printf("Press 0 to exit\n\n");
          scanf("%d", &choice);
       }
    }
    
    //***************************************************
    
    int read_studentRecord ( studentData details[] ) 
    { 
    int i = 0;
    int recordNum; 
    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 ); 
    
    recordNum = i+1;
    
    i++; 
    
    printf ( "Enter Student Number\n?"); 
    scanf ( "%s", details[i].studentNum); 
    } 
    system("cls");
    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%-10s%-16s%-11s%10s\n\n","Record #","Student #","Last Name","First Name","Phone #"); 
    
    while ( details[i].studentNum[0] != '0' ) 
    { 
    printf("%-10d%-10s%-16s%-11s%10s\n",i+1, details[i].studentNum, details[i].lastName, details[i].firstName, details[i].phoneNum); 
    
    i++; 
    
    
    
    } 
    printf("\n\n");
    printf("Press any key to return to the main menu\n");
    getch();
    system("cls");
    } 
    
    // ************************************************** 
    //*************************************** 
    
    
    
    void search_studentRecord( studentData details[], int numOfRec ) 
    { 
    int low, high, mid; 
    char searchkey[10]; 
    int flag = 0; 
    int i = 0;
    printf ("\n%-10s%-10s%-16s%-11s%10s\n\n","Record #","Student #","Last Name","First Name","Phone #");
    while ( details[i].studentNum[0] != '0' ) 
    { 
    printf("%-10d%-10s%-16s%-11s%10s\n",i+1, details[i].studentNum, details[i].lastName, details[i].firstName, details[i].phoneNum); 
    
    i++; 
    
    } 
    
    printf("\n\n\nEnter 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("Record Found\n\n");
    printf("Details for %s:", searchkey);
    printf("\n\n\tStudent I.D: %s\n\tPhone Number: %s\n", details[mid].studentNum, details[mid].phoneNum); 
    flag = 1; 
    break; 
    } 
    } 
    
    if (!flag) printf("Search Key not found\n"); 
    printf("\n\nPress any key to return to the main menu\n");
    getch();
    system("cls");
    } 
    
    //*********************************************************************
    //********************************************************
    
    
    int delete_studentRecord(studentData details[], int n)
    
    {
    
    int delete_index;
    int last_index = n;
    int i = 0;
    printf ("\n%-10s%-10s%-16s%-11s%10s\n\n","Record #","Student #","Last Name","First Name","Phone #");
    while ( details[i].studentNum[0] != '0' ) 
    { 
    printf("%-10d%-10s%-16s%-11s%10s\n",i+1, details[i].studentNum, details[i].lastName, details[i].firstName, details[i].phoneNum); 
    
    i++; 
    
    } 
    
    printf("max is %d\n", last_index);
    
    printf("Enter a record number to delete the details of:\n");
    scanf("%d", &delete_index);
    
    printf("delete index is %d\n", delete_index);
    
    if(delete_index != n)
    {
       // 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
       // the last record is deleted because it has already been copied over
       details[last_index].Deleted = 1;
    }
    else
       details[delete_index].Deleted = 1;
       printf("Last index gone\n\n");
    --last_index;
    
    return 0;
    
    }
    

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    else
       details[delete_index].Deleted = 1;
       printf("Last index gone\n\n");
    --last_index;
    It's always the simple things. You forgot your braces block around this.

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Aug 2002
    Posts
    34
    Thanks for pointing that out Quzah, it is always the little things. however - same problem

    For example if there are 5 records then the last record would be [4] correct? If i want to get rid of the last record and i input record 5 as the item to be deleted, it does nothing, but if i say record 4 then it gets rid of the last one.
    I have no idea where this is happening?
    Can anyone spot it?

  4. #4
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Just to clarify... you do realise that the array size is static? You can't actually remove an element from it at all, but you can ignore one.

    >For example if there are 5 records then the last record would be [4] correct?
    Yes. The array is like this

    a[0], a[1].... a[4] <- 5 elements, numbered 0 to 4.

    So if the user enters a choice of 1 to 5, you need to decrement that input by one to access the required element of the array.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

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

    Thumbs up

    Thanks hammer, you are a qualified champion.

  6. #6
    Unleashed
    Join Date
    Sep 2001
    Posts
    1,765
    Those numbering schemes are tricky little buggers.
    The world is waiting. I must leave you now.

  7. #7
    Green Member Cshot's Avatar
    Join Date
    Jun 2002
    Posts
    892
    Thanks hammer, you are a qualified champion
    haha, you should put that in your signature, hammer
    Try not.
    Do or do not.
    There is no try.

    - Master Yoda

  8. #8
    Registered User
    Join Date
    Sep 2002
    Posts
    137
    Don't know if this is any help ( the code looks okay to me, but I'm new ) but I've written an alternative to your big if () else if () cycle.
    It looks a big better to read if nothing else:

    void menu (void)
    {
    int n;
    int choice;
    studentData details [MAX_STUDENTS]; //define a structure array of size MAX_STUDENTS

    printf("Press 1 to Enter Records\n\n");
    printf("Press 2 to View records in sorted form\n\n");
    printf("Press 3 to Search for a record\n\n");
    printf("Press 4 to Delete a record\n\n");
    printf("Press 0 to exit\n\n");
    scanf("%d", &choice);

    while ( choice != 0 ){

    switch (choice) {

    case '1' :
    system("cls");
    n = read_studentRecord( details );
    break;

    case '2' :
    system("cls");
    sort_studentRecord( details, n );
    print_studentRecord( details );
    break;

    case '3' :
    system("cls");
    search_studentRecord( details, n );
    break;

    case '4' :
    system("cls");
    delete_studentRecord( details, n );
    break;

    default :
    break;
    }
    }
    printf("Invalid input\n");
    printf("Press 1 to Enter Records\n\n");
    printf("Press 2 to View records in sorted form\n\n");
    printf("Press 3 to Search for a record\n\n");
    printf("Press 4 to Delete a record\n\n");
    printf("Press 0 to exit\n\n");
    scanf("%d", &choice);
    }
    }
    http://uk.geocities.com/ca_chorltonkids

  9. #9
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>It looks a big better to read if nothing else:
    It'd look even better in code tags!

    You're doing the case statement slightly incorrect. You need something like
    >case 1:
    not
    >case '1':

    This is because you read the input as an int through scanf().

    And if you're going to clear the screen for all the choices, why not just put that line of code just above the switch statement?

    And a note to all the scanf() users.... always check the return code from scanf() to ensure it did what you asked.

    >haha, you should put that in your signature, hammer
    Yeah, it did make me smile !
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  10. #10
    Registered User
    Join Date
    Aug 2002
    Posts
    34
    Does anyone know if there is a place that i can get information on how to comment in a program properly? and overall documentation.

    Thanks Guys,

    Simon

  11. #11
    Unleashed
    Join Date
    Sep 2001
    Posts
    1,765
    That's a personal preference really.
    Would you let somebody else tell you how to tie your shoes?
    The world is waiting. I must leave you now.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 01-05-2008, 11:30 PM
  2. Absolute value of each element in a given array
    By DriftinSW20 in forum C Programming
    Replies: 9
    Last Post: 11-15-2007, 04:08 PM
  3. delete an element from a linked list
    By hinman in forum C Programming
    Replies: 6
    Last Post: 10-17-2007, 08:30 PM
  4. Replies: 6
    Last Post: 11-09-2006, 03:28 AM
  5. Deleting element from an array problem
    By xamlit in forum C Programming
    Replies: 5
    Last Post: 12-03-2005, 04:53 PM