1
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

しかし、たとえばブライアンを削除しようとすると、削除されますが、次のようになります

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

その空の配列を最後の位置に移動して、「Height_of_array」をデクリメントして、保存可能な学生の数が1減って削除を反映できるようにする方法はありますか

4

1 に答える 1

0

あなたのコードにはいくつかの問題があります: それは私に飛びつきます:

 fflush(stdin);

絶対にしないでください。出力ストリームのみをフラッシュします。

height_of_array = height_of_array--;

未定義の動作です。つまり、次のことを意味します。

height_of_array--;
于 2012-05-28T16:08:16.700 に答える