如何将空白数组排序到顶部,以便我可以删除它们

时间:2012-05-28 16:00:16

标签: c arrays

int id_swap()
{
    char tempstring[15];

    strcpy(tempstring, id[index+1]);
    strcpy(id[index+1], id[index]);
    strcpy(id[index], tempstring);
}

int delete_student()
{
    int found=0;
    char id_to_find[10];

    printf("Please enter the student ID to delete\n\n");
    scanf("%s", & id_to_find);
    fflush(stdin);
    system("cls");

    for(index=0;index<height_of_array+1;index++)
    {
        if(strcmpi(id_to_find, id[index]) == 0)
        {
            found=1;
            id_swap();
            system("cls");
            printf("Student deleted");
            height_of_array = height_of_array--;
        }

好的,这是我的代码的一部分。发生了什么的一个简单例子

我对已经加入该计划的学生进行排序,以便像这样出现,例如

Student#1 Chris ID: 1831
Student#2 etc
student#3 etc
student#4 Brian ID: 4432
student#5 etc
student#6 etc

但是当我试图删除说Brian的例子时,它删除了它,但它看起来像这个

student#1 ID:
Student#2 Chris ID:1831
Student#3 etc

有没有办法将空白数组移动到最后一个位置,这样我就可以减少我的“Height_of_array”,以便可存储学生的数量减少1来反映删除

1 个答案:

答案 0 :(得分:0)

您的代码存在一些问题:向我跳过:

 fflush(stdin);

永远不要那样做。只刷新输出流。

height_of_array = height_of_array--;

未定义的行为,您的意思是:

height_of_array--;