我正在嘗試編寫一個程序,它將從用戶處獲取信息,用戶可以選擇要執行的操作。選項是添加,修改,刪除,顯示某些信息,查找平均值,然後退出。 我已經爲它們編寫了代碼,但無法使我的刪除功能正常工作。 這裏是我的代碼:當我使用「刪除」功能時,程序會掛斷
void delete_student(struct students *list, int *count) //Delete's Selected student
{
int id4;
int k;
if (*count != 0)
{
printf("\nEnter the ID of the student you wish to delete: ");
scanf("%d\n", &id4);
for (int i = 0; i < *count; i++) //Searches for the selected student
{
k = i;
if (list[i].id == id4)
{
printf("\nStudent found.\nDeleting...\n");
for (int c = k; c < *count; c++) //Deletes the student if found
{
list[c].id = list[c + 1].id;
strcpy(list[c].name, list[c + 1].name);
list[c].age = list[c + 1].age;
strcpy(list[c].dept, list[c + 1].dept);
list[c].grade = list[c + 1].grade;
}
*count = *count - 1;
printf("\nDeletion Complete.\n");
break;
}
else
printf("\nStudent not found. Please try again.\n");
break;
}
}
else
printf("\nNo student's to delete.\n");
}
編輯: 當我經過的程序,我選擇使用此功能,它會問我的「ID」,然後什麼也不做,並具有眨眼光標。
有人能告訴我我做錯了什麼嗎? 有更多的代碼,如果你需要它。
請問這是怎麼的最後一個元素將被刪除:
list[*count].id = 0;
strcpy(list[*count].name, NULL);
list[*count].age = 0;
strcpy(list[*count].dept, NULL);
list[*count].grade = 0;
請說明爲什麼這不起作用。你是否收到編譯器錯誤?運行時出現分段錯誤?代碼是否運行,但不像它應該那樣運行? –
@ZachStark當我瀏覽程序時,我選擇使用這個函數,它會問我「ID」,然後它什麼都不做,並且有一個閃爍的光標。 –