要實現修改功能,首先需要獲取用戶輸入的數據,并根據用戶輸入的信息找到對應的數據進行修改。以下是一個簡單的示例代碼來實現修改功能:
#include <stdio.h>
#include <string.h>
struct Student {
char name[50];
int age;
};
int main() {
struct Student students[10];
int numStudents = 0;
int choice, index;
do {
printf("\n1. Add student\n");
printf("2. Modify student\n");
printf("3. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch(choice) {
case 1:
printf("\nEnter student name: ");
scanf("%s", students[numStudents].name);
printf("Enter student age: ");
scanf("%d", &students[numStudents].age);
numStudents++;
break;
case 2:
printf("\nEnter index of student to modify (0-%d): ", numStudents-1);
scanf("%d", &index);
if (index >= 0 && index < numStudents) {
printf("Enter new name for student: ");
scanf("%s", students[index].name);
printf("Enter new age for student: ");
scanf("%d", &students[index].age);
} else {
printf("Invalid index\n");
}
break;
case 3:
break;
default:
printf("Invalid choice\n");
}
} while (choice != 3);
printf("\nList of students:\n");
for (int i = 0; i < numStudents; i++) {
printf("%d. %s, %d\n", i, students[i].name, students[i].age);
}
return 0;
}
這段代碼定義了一個學生結構體Student
,可以添加學生信息和修改學生信息。用戶可以選擇添加學生信息、修改學生信息或退出程序。程序會一直運行直到用戶選擇退出。