0

I am writing a student report card system, and while implement a if-else grading system starting from " if(st[i].per>=80){", i am getting an error saying [Error] assignment to expression with array type, how do i tackle it and solve it ? i have to submit it this week, i solved all the other problems, and i am sorry if i didnt indent it properly in stack.

    #include<stdio.h>
    #include<conio.h>
    #include<stdlib.h>
    #include<math.h>
    #include<string.h>
    int i=0;
    struct student{
        char fName[50];
        char lName[50];
        char FatherName[50];
        char Branch[50];
        char Div[12];
        int roll, engmech, epc, chem, pps, maths;
        float per;
        char grade[100];
        int cid[10];
    }st[55];

    void add_student(){
        printf("\nAdd New Student Info");
        printf("\n--------------------");
        printf("\nEnter Student's First Name:");
        scanf("%s", st[i].fName);
        printf("\nEnter Student's Last Name:");
        scanf("%s", st[i].lName);
        printf("\nEnter Roll Number:");
        scanf("%d", &st[i].roll);
        printf("\nEnter Branch:");
        scanf("%s", st[i].Branch);
        printf("\nEnter Division:");
        printf("%s", st[i].Div);
        printf("\nEnter Marks for Engineering Mechanics:");
        scanf("%d", &st[i].engmech);
        printf("\nEnter Marks for Mathematics:");
        scanf("%d", &st[i].maths);
        printf("\nEnter Marks for Computer Science:");
        scanf("%d", &st[i].engmech);
        printf("\nEnter Marks for English:");
        scanf("%d", &st[i].epc);
        printf("\nEnter Marks for Chemistry:");
        scanf("%d", &st[i].chem);
        st[i].per=st[i].engmech+st[i].maths+st[i].epc+st[i].chem+st[i].pps/5.0 *100;
        if(st[i].per>=80){
            st[i].grade="A";    
        }
        else if(st[i].per>=60){
            st[i].grade="B";
        }
        else if(st[i].per>=40){
            st[i].grade="C";
        }
        else if(st[i].per<40){
            st[i].grade="F";
        }   
        for(int j=0;j<5;j++){
            scanf("%d", &st[i].cid[j]);
        }
    
        i=i+1;
    }
    void find_Rstudent()
    {                   
        int x;
        printf("\nEnter the Roll Number of the Student:");
        scanf("%d", &x);
        for(int j=1;j<i;j++){
            if (x==st[i].roll){
                printf("\nHere are your search Results");
                printf("\nName of the student:%s %s %s",st[i].fName, st[i].FatherName, st[i].lName);
                printf("\nBranch_Class:%s %s", st[i].Branch, st[i].Div);
                printf("\nRoll No:%d", st[i].roll);
                printf("\nMarks Obtained in Engineering Mechanics:%d", st[i].engmech);
                printf("\nMarks Obtained in Mathematics:%d", st[i].engmech);
                printf("\nMarks Obtained in Computer Science:%d", st[i].pps);
                printf("\nMarks Obtained in English:%d", st[i].epc);
                printf("\nMarks Obtained in Chemistry:%d", st[i].chem);
                printf("\nPercentage Scored: %f",st[i].per);
                printf("\nGrade Obtained:%s", st[i].grade);  
            }
            for(int j=0;j<5;j++){
                printf("\nThe course ID is:%d",st[i].cid);
            }
            break;
        }
    }
    void update_student(){
        printf("\nEnter Roll Number to update Entry:");
        long int x;
        scanf("%ld", &x);
        for (int j=0;j<i;j++){
            if(st[j].roll==x){
                printf("\n1.First Name");
                printf("\n2.Father's Name:");
                printf("\n3.Roll No.");
                printf("\n4.Percentage");
                printf("\n5.Grade");
            int z;
            scanf("%d", &z);
            switch(z){
                case 1:
                    printf("\nEnter New First Name:");
                    scanf("%s",st[i].fName);
                    break;
                case 2:
                    printf("\nEnter New Father's Name:");
                    scanf("%s", st[i].FatherName);
                    break;
                case 3:
                    printf("\nEnter New Roll no:");
                    scanf("%d", &st[i].roll);
                    break;
                case 4:
                    printf("\nEnter New Percentage:");
                    scanf("%f", &st[i].per);
                    break;
                case 5:
                    printf("\nEnter New Grade:");
                    scanf("%s", &st[i].grade);
                    break;
            }
            printf("\nUpdated Successfully");   
            }
        }
    }
    void main()
    {
        printf("\nWelcome to Knight's College of Engineering and Technology");
        int choice, count;
        while(i=1){
            printf("\nWhat task do you want to perform ?");
            printf("\n1.Add a new student details");
            printf("\n2.Find an existing Student");
            printf("\n3.Modify an exisitng Student Info");
            printf("\n4.Exit");
            scanf("%d", &choice);
            switch(choice){
                case 1:
                    add_student();
                    break;
                case 2:
                    find_Rstudent();
                    break;
                case 3:
                    update_student();
                    break;
                case 4:
                    exit(0);
                    break;
            
            }
        }
    }
  • Does this answer your question? [Assigning strings to arrays of characters](https://stackoverflow.com/questions/579734/assigning-strings-to-arrays-of-characters) and ["error: assignment to expression with array type error" when I assign a struct field (C)](https://stackoverflow.com/questions/37225244/error-assignment-to-expression-with-array-type-error-when-i-assign-a-struct-f) – user17732522 Jun 17 '22 at 19:52

1 Answers1

0

On this line:

st[i].grade="A";

The grade member is a char array, and arrays cannot be assigned to. If you want to copy strings, you need to use strcpy:

strcpy(st[i].grade,"A");
dbush
  • 205,898
  • 23
  • 218
  • 273