I need to create a program in which a simply Singly linked list of students will be created. And I need to make it possible to change the name of a given student. When compiling, errors appear:
Severity Code Description Project File Line Suppression State
Error (active) E0137 expression must be a modifiable lvalue Student Work C:\Users\John\source\repos\Student Work\Student Work\main.c 87
Error (active) E1072 a declaration cannot have a label Student Work C:\Users\John\source\repos\Student Work\Student Work\main.c 111
Error C2106 '=': left operand must be l-value Student Work C:\Users\John\source\repos\Student Work\Student Work\main.c 87
How can I fix these errors? And further, advise how I can improve my code? Maybe something can be written easier, better or more efficiently? 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("Enter 1 student name: ");
scanf("%s", (*student)->Name);
printf("Enter 1 student age: ");
scanf("%d", &(*student)->Age);
printf("Enter 1 student average raiting: ");
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("enter %d student name: ", i);
scanf("%s", endStudent->Name);
printf("Enter %d student age: ", i);
scanf("%d", &endStudent->Age);
printf("Enter %d student average raiting: ", i);
scanf("%f", &endStudent->AverageRaiting);
printf("\n");
endStudent->nextStudent = NULL;
}
}
void PrintList(struct Student* student)
{
struct Student* printStudent = student;
printf("==========================\n");
printf("Number Name Age average raiting \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("Changes are saved");
break;
}
}
}
int main(void)
{
int command;
struct Student* BaseStudent = NULL;
SetConsoleCP(1251);
SetConsoleOutputCP(1251);
InitStudentList(&BaseStudent);
for (;;)
{
printf("Enter command:\n 1 - Show student list,\n 2 - change student name,\n 3 - exit\n");
scanf("%d", &command);
switch (command)
{
case 1:
PrintList(BaseStudent);
break;
case 2:
int n;
char name[KOL];
printf("Enter student number: ");
scanf("%d", &n);
printf("Enter new student name: ");
scanf("%s", &name);
ChangeStudentName(n, name, BaseStudent);
break;
case 3:
FreeList(&BaseStudent);
return 0;
break;
default:
printf("Error...");
FreeList(&BaseStudent);
return 0;
break;
}
}
return 0;
}
Thanks in advance!