0

i have this so far and i need help to modify a student info on its own. I have tried several ways and i cant seem to figure it out.

#include <stdio.h>

struct student
{
    int id;
    char name[50];
    int age;
    char dept[10];
    float grade;
};

int main()
{
    struct student s[10];

    int i;

    printf("Enter Student Information:\n");

    for (i=0; i<10; i++)
    {
        s[i].id = i+1;
        printf("\nFor ID number %d\n",s[i].id);
        printf("Enter name: "); scanf("%s",s[i].name);
        printf("Enter age: "); scanf("%d", &s[i].age);
        printf("Enter department: "); scanf("%s", s[i].dept);
        printf("Enter grade: "); scanf("%f",&s[i].grade);
        printf("\n");
    }

    return 0;
}
4

1 回答 1

1

对于您想要的东西,可能有点过头了,但谁在数:

#include <stdio.h>
#include <string.h>

struct student {
    int id;
    char name[50];
    int age;
    char dept[10];
    float grade;
};

void print_student(struct student * students, size_t num_students, int id);
void set_student_by_id(struct student * students, size_t num_students,
                       int id, struct student * new_info);


int main(void) {
    struct student students[5] = {
        {100, "John", 17, "Physics", 3.2},
        {101, "Emily", 16, "Maths", 3.8},
        {102, "Peter", 18, "Drama", 2.1},
        {103, "Doug", 16, "English", 3.3},
        {104, "Sara", 17, "Astronaut", 3.9}
    };

    size_t num_students = sizeof(students) / sizeof(students[0]);

    /*  Print details for student 101 before change  */

    print_student(students, num_students, 101);

    /*  Change details for student 101  */

    struct student new_info = {-1, "Emily", 17, "Farming", 3.9};
    set_student_by_id(students, num_students, 101, &new_info);

    /*  Print details for student 101 after change  */

    print_student(students, num_students, 101);

    /*  Attempt to print details for imaginary student 105  */

    print_student(students, num_students, 105);

    return 0;
}


/*
 *  Prints the details for a selected student.
 *
 *  Arguments:
 *    students - pointer to an array of students
 *    num_students - number of students in the array
 *    id - ID of the student to print
 */

void print_student(struct student * students, size_t num_students, int id) {
    size_t i;
    for ( i = 0; i < num_students; ++i ) {

        /*  Search array for student by ID  */

        if ( students[i].id == id ) {
            printf("Info for student ID %d:\n", id);
            printf("  Name: %s\n", students[i].name);
            printf("  Age: %d\n", students[i].age);
            printf("  Dept: %s\n", students[i].dept);
            printf("  Grade: %.1f\n", students[i].grade);

            break;
        }
    }

    if ( i == num_students ) {
        printf("Student ID %d not found.\n", id);
    }
}


/*
 *  Changes the details for a selected student.
 *
 *  Arguments:
 *    students - pointer to an array of students
 *    num_students - number of students in the array
 *    id - ID of the student to change
 *    new_info - pointer to struct student containing new details
 */

void set_student_by_id(struct student * students, size_t num_students,
                       int id, struct student * new_info) {
    size_t i;
    for ( i = 0; i < num_students; ++i ) {

        /*  Search array for student by ID  */

        if ( students[i].id == id ) {

            /*  Change everything except the ID  */

            strcpy(students[i].name, new_info->name);
            strcpy(students[i].dept, new_info->dept);
            students[i].age = new_info->age;
            students[i].grade = new_info->grade;

            break;
        }
    }

    if ( i == num_students ) {
        printf("Student ID %d not found.\n", id);
    }
}

输出:

paul@local:~/src/c/scratch$ ./student
Info for student ID 101:
  Name: Emily
  Age: 16
  Dept: Maths
  Grade: 3.8
Info for student ID 101:
  Name: Emily
  Age: 17
  Dept: Farming
  Grade: 3.9
Student ID 105 not found.
paul@local:~/src/c/scratch$
于 2013-10-25T03:33:52.913 回答