Reputation: 23
I'm new to C, and honestly have no idea where to start with removing a particular element from an array of structures.
If you wish, you can view and copy my code in its entirety here: http://pastebin.com/Zbrm2xyL
Mostly I'm concerned with the function 'rmv_student', which is supposed to remove the struct with a matching id number from the array 'st_array' without messing with the other elements of that array after prompting the user for confirmation. Function 'rmv_student' is as follows:
void rmv_student(long id) // BROKEN
{
int i; // iterator
char response; // used to confirm deletion
for( i = 0; i < MAX; i++){
if ( st_array[i].id == id){
printf("Are you sure you want to delete %s %s, %d?\n", st_array[i].first_name, st_array[i].last_name, st_array[i].id);
puts("You will not be able to undo the deletion.");
puts("Enter 'y' to delete or 'n' to return to the main menu.");
response = getchar();
switch (response){
case 'y':
// delete
case 'Y':
// delete
case 'n':
main();
case 'N':
main();
default:
puts("Please enter 'y' or 'n'.");
rmv_student(id);
}
}
}
if ( i == MAX ){
printf("\nThere are no students with ID %d.\n\n", id);
main();
}
}
I have two questions.
Are my switch cases correct? Will this test the user's input character correctly?
How do I go about deleting the struct?
Before you ask. Yes, this is homework. As such, I'm not looking for a handout, just a point in the right direction. Any other suggestions are welcome.
Note: I am aware that I don't really need the function 'menu_test_input', but I'm leaving it for now.
Upvotes: 2
Views: 8788
Reputation: 40604
There are two possible solutions to your problem, which one you should use depends on whether the order of the array elements is important to you.
The fast solution: Copy the last element in the array to the position of the element you want to delete, then simply decrement your count of elements in the array.
int* array = ...;
int elementCount = ...;
...
int deletionIndex = ...;
array[deletionIndex] = array[--elementCount]; //the deletion is actually a one liner :-)
This solution is the preferred one whenever you are operating with an unsorted array, it takes only a constant amount of time, regardless of where you do the deletion.
The long solution: Move all elements behind the deleted element one position to the front.
//setup is the same as for the fast solution
elementCount--;
for(int i = deletionIndex; i < elementCount; i++) array[i] = array[i+1];
Not exactly difficult, but considerably more complex than the fast solution.
You need to use this whenever you need to preserve the relative order of the array elements. The price for the ordering is that the runtime depends on the amount of elements that need to be moved.
Upvotes: 1
Reputation: 409166
Use loops and return statements instead of recursive calling! Remember that when the called function returns the code will continue after the call.
Instead do something like the following pseudo-code
do
{
print_prompt()
get_response()
} while (response is not legal)
if (response is yes)
do_the_actual_deletion
If you want to remove element X of array A, then move the element X + 1 to X, move element X + 2 to X + 1, etc. When done then decrease the size by one. No actual "removing" involved.
Upvotes: 1
Reputation: 63
you have to use break;
case 'y':
//your code
break;
case 'Y':
//your code
break;
case 'n':
break;
...
......
or the code will run all your cases.
proper use - http://www.tutorialspoint.com/cprogramming/switch_statement_in_c.htm
Upvotes: 0