1

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!

the busybee
  • 10,755
  • 3
  • 13
  • 30
  • You cannot have variable declaration as first line in `case` body, this is a limitation of C syntax. Just wrap the body of `case 2` into block. – qrdl May 28 '20 at 05:15
  • This line is wrong: "changingStudent->Name = name;". You should use strcpy [link](https://learn.microsoft.com/en-us/cpp/c-runtime-library/reference/strncpy-strncpy-l-wcsncpy-wcsncpy-l-mbsncpy-mbsncpy-l?view=vs-2019) instead. – mar May 28 '20 at 05:21
  • There is a bug in function ChangeStudentName() - if student of given id ('n') is not found the loop is a endless loop. – mar May 28 '20 at 05:25
  • changingStudent->Name = name You can't copy one array to other array use strcpy or memcpy – Malik Ji May 28 '20 at 05:27
  • How can i fix changingStudent->Name = name – John Berden May 28 '20 at 05:30
  • In the function, I did this: const int size=sizeof(changingStudent->Name); if(size && size>=sizeof(name)) { memcpy(changingStudent->Name,name,size); changingStudent->Name[size-1]='\0'; } But when executed, I don’t even get an output about a successful change. The program freezes. In the function, I tried: strcpy(changingStudent->Name,name); But the program also freezes, and I cannot understand why. – John Berden May 28 '20 at 08:01

1 Answers1

0

There are two problems in your code:

  1. In your ChangeStudentName() method,you cannot copy one array to another directly. You can copy one char array to other char array using strcpy() method.

    e.g- strcpy(changingStudent->Name,name);

  2. In your main() method,

    In case 2:,you should not declare variables in switch statements like this.

Check this link for more info:Why can't variables be declared in a switch statement?

If you want to declare variables inside switch statements,then you should use braces as shown below.

e.g-

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

Hope it helps!!

Amit Nandal
  • 118
  • 1
  • 6
  • In the function, I did this: const int size=sizeof(changingStudent->Name); if(size && size>=sizeof(name)) { memcpy(changingStudent->Name,name,size); changingStudent->Name[size-1]='\0'; } But when executed, I don’t even get an output about a successful change. The program freezes. In the function, I tried: strcpy(changingStudent->Name,name); But the program also freezes, and I cannot understand why. – John Berden May 28 '20 at 08:01
  • Can you tell me,how you use strcpy()? Edit your question and paste the new code in which you use strcpy(), i will check it. – Amit Nandal May 28 '20 at 08:12
  • The `{` and `}` are called braces, not parentheses (at least in this context). – Eric Postpischil May 28 '20 at 10:26
  • 1
    The phrasings “you cannot declare variables in switch statements” and “if you want to declare variables inside switch statements then…” are contradictory. If you cannot do something, there is no way to do it. Instead, the first should say a declaration cannot be the first thing after a `case` label, and the second should say one can instead use a compound statement (`{ … }`) after a `case` and put a declaration inside that. – Eric Postpischil May 28 '20 at 10:30