B
B
Ben_r0072020-05-27 10:48:18
C++ / C#
Ben_r007, 2020-05-27 10:48:18

Singly linked linear list of students. How to fix the error?

Hello.
You need to create a program that will create a singly linked linear list of students. And you need to make it possible to change the name of a given student.
When compiling, the following errors are output:
Severity Code Description Project File String Suppression Status
Error (active) E0137 expression must be valid for change by left-hand value Student Work C:\Users\John\source\repos\Student Work\Student Work\main.c 87
Error (active) E1072 Ad cannot be assigned the tag Student Work C:\Users\John\source\repos\Student Work\Student Work\main.c 111
Error C2106 =: Left operand must be left hand value Student Work C:\Users \John\source\repos\Student Work\Student Work\main.c 87

How can these errors be corrected?
And additionally, how can I improve my code? Maybe something can be written easier, better or more efficient?
main.c code:

#include <stdio.h>
#include <string.h>
#include <windows.h>
#define MAX 3
#define KOL 15

struct Student
{
    int id;
    char Name[KOL];
    int Age;
    float AverageRaiting;
    struct Student* nextStudent;
};

void InitStudentList(struct Student** student)
{
    *student = (struct Student*)
        malloc(sizeof(struct Student));
    (*student)->id = 1;
    printf("Введіть ім’я 1-го студента: ");
    scanf("%s", (*student)->Name);
    printf("Введіть вік 1-го студента: ");
    scanf("%d", &(*student)->Age);
    printf("Введіть середній рейтинг 1-го студента: ");
    scanf("%f", &(*student)->AverageRaiting);
    printf("\n");
    (*student)->nextStudent = NULL;
    struct Student* endStudent = *student;
    for (int i = 2; i <= MAX; i++)
    {
        endStudent->nextStudent =
            (struct Student*) malloc(sizeof(struct Student));
        endStudent = endStudent->nextStudent;
        endStudent->id = i;
        printf("Введіть ім’я %d-го студента: ", i);
        scanf("%s", endStudent->Name);
        printf("Введіть вік %d-го студента: ", i);
        scanf("%d", &endStudent->Age);
        printf("Введіть середній рейтинг %d-го студента: ", i);
        scanf("%f", &endStudent->AverageRaiting);
        printf("\n");
        endStudent->nextStudent = NULL;
    }
}

void PrintList(struct Student* student)
{
    struct Student* printStudent = student;
    printf("==========================\n");
    printf("Номер         Ім’я         Вік  Рейтинг \n");
    printf("==========================\n");
    while (printStudent)
    {
        printf("%d", printStudent->id);
        printf("%-15s", printStudent->Name);
        printf("%4d", printStudent->Age);
        printf("%8.2f", printStudent->AverageRaiting);
        printf("\n");
        printStudent = printStudent->nextStudent;
    }
    printf("==========================\n");
}

void FreeList(struct Student** student)
{
    if (*student == NULL)
        return;
    struct Student* tmp = *student;
    struct Student* curr_stud;
    while (tmp)
    {
        curr_stud = tmp;
        tmp = tmp->nextStudent;
        free(curr_stud);
    }
    *student = NULL;
}

void ChangeStudentName(int n, char name[KOL], struct Student* student)
{
    struct Student* changingStudent = student;
    while (changingStudent)
    {
        if (changingStudent->id == n)
        {
            changingStudent->Name = name;
            printf("зміни записані");
            break;
        }
    }
}

int main(void)
{
    int command;
    struct Student* BaseStudent = NULL;
    SetConsoleCP(1251);
    SetConsoleOutputCP(1251);
    InitStudentList(&BaseStudent);
    for (;;)
    {
        printf("Введіть команду:\n 1 - Виведення списку студентів,\n 2 - Змінити прізвище зазначеного студента,\n 3 - вихід\n");
        scanf("%d", &command);
        switch (command)
        {
        case 1:
            PrintList(BaseStudent);
            break;
        case 2:
            int n;
            char name[KOL];
            printf("Введіть номер студента: ");
            scanf("%d", &n);
            printf("Введіть нове ім'я студента: ");
            scanf("%s", &name);
                ChangeStudentName(n, name, BaseStudent);
            break;
        case 3:
            FreeList(&BaseStudent);
            return 0;
            break;
        default:
            printf("Помилка вводу...");
            FreeList(&BaseStudent);
            return 0;
            break;
        }
    }
    return 0;
}


Thanks in advance!

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question