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
  • what did you try? My suggestion is if you want to modify the information of student number 3, you could do that by s[2].id = 5, s[2].name = <something>, etc. Commented Oct 25, 2013 at 0:38
  • 2
    What's the outcome and what do you expect as outcome? I mean you wrote some nice code. Where exactly does the problem start? Commented Oct 25, 2013 at 0:38
  • @DanielS. for instance after inputing the student info, you can select one student and change maybe the name or the grade Commented Oct 25, 2013 at 1:12
  • @lightbringer let me try that Commented Oct 25, 2013 at 1:14

1 Answer 1

1

Possibly a little over-the-top for what you want, but who's counting:

#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);
    }
}

Outputs:

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$
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.