assignment to char from char * makes integer from pointer without a case

798 views Asked by At

I need to make a program that calculates and grades any number of students based on the inputs. I also need to print the top 3 students after it is graded and display the final grade in Letter format. I was wondering what I did wrong in the Letter grade because this kept popping as an error: Error for letter grade

Here is my code so far:

#include <stdio.h>

struct student {
    char firstName[50], lastName[50], equigrade;
    int q1, q2, q3, q4, q5, midterms, finals;
    float FinalGrade;
} s[15];

int main() {
    int i, n, q1, q2, q3, q4, q5, midterms, finals, TotalQuiz;
    float QuizGrade, MidtermExamGrade, FinalExamGrade, FinalGrade;
    printf("Enter number of students:");
    scanf("%d",&n);
    printf("\nENTER STUDENT'S INFORMATION\n");

// storing information
for (i = 0; i < n; ++i) 
{
    printf("\nEnter Student's Full Name: ");
    scanf("%s %s", s[i].firstName, s[i].lastName);
    printf("SCORES\n");
    printf("Quiz 1: ");
    scanf("%d", &s[i].q1);
    printf("Quiz 2: ");
    scanf("%d", &s[i].q2);
    printf("Quiz 3: ");
    scanf("%d", &s[i].q3);
    printf("Quiz 4: ");
    scanf("%d", &s[i].q4);
    printf("Quiz 5: ");
    scanf("%d", &s[i].q5);
    printf("Midterms (100pts): ");
    scanf("%d", &s[i].midterms);
    printf("Finals (100pts): ");
    scanf("%d", &s[i].finals);
    printf("\n");
}

//calculation
TotalQuiz = q1 + q2 +  q3 + q4 + q5;

QuizGrade = (TotalQuiz/50)*100*0.50;
MidtermExamGrade = (midterms/100)*100*0.20;
FinalExamGrade = (finals/100)*100*0.30;
FinalGrade = QuizGrade + MidtermExamGrade + FinalExamGrade;

// alphabetic equivalent
for(i = 0; i < n; i++)
{
    if(s[i].FinalGrade>=97 && s[i].FinalGrade<=100)
    {
        s[i].equigrade = "A+";
    }
    else if(s[i].FinalGrade>=93 && s[i].FinalGrade<=96)
    {
        s[i].equigrade = 'A';
    }
    else if(s[i].FinalGrade>=90 && s[i].FinalGrade<=92)
    {
        s[i].equigrade = "A-";
    }
    else if(s[i].FinalGrade>=87 && s[i].FinalGrade<=89)
    {
        s[i].equigrade = "B+";
    }
    else if(s[i].FinalGrade>=83 && s[i].FinalGrade<=86)
    {
        s[i].equigrade = 'B';
    }
    else if(s[i].FinalGrade>=80 && s[i].FinalGrade<=82)
    {
        s[i].equigrade = "B-";
    }
    else if(s[i].FinalGrade>=77 && s[i].FinalGrade<=79)
    {
        s[i].equigrade = "C+";
    }
    else if(s[i].FinalGrade>=73 && s[i].FinalGrade<=76)
    {
        s[i].equigrade = 'C';
    }
    else if(s[i].FinalGrade>=70 && s[i].FinalGrade<=72)
    {
        s[i].equigrade = "C-";
    }
}


printf("Displaying Information:\n\n");

// displaying information
for (i = 0; i < n; ++i) 
{
    printf("First name: %s %s\n", s[i].firstName, s[i].lastName);
    printf("Marks: %d\n", s[i].q1);
    printf("Marks: %d\n", s[i].q2);
    printf("Marks: %d\n", s[i].q3);
    printf("Marks: %d\n", s[i].q4);
    printf("Marks: %d\n", s[i].q5);
    printf("\n");
}
return 0;

}

1

There are 1 answers

6
Vlad from Moscow On

According to the declaration

char firstName[50], lastName[50], equigrade;

the data member equigrade has the type char. But in statements like this

s[i].equigrade = "A+";

you are trying to assign a pointer to a string literal to a character. That is the string literal "A+" has the character array type char[3] that used as an initializer is converted to a pointer to its first element of the type char *.

Redeclare the variable like a pointer

char firstName[50], lastName[50], *equigrade;

And everywhere in such statements use string literals instead of integer character constants. That is instead of for example this assignment

s[i].equigrade = 'A';

write

s[i].equigrade = "A";

Pay attention to that in these statements

QuizGrade = (TotalQuiz/50)*100*0.50;
MidtermExamGrade = (midterms/100)*100*0.20;
FinalExamGrade = (finals/100)*100*0.30;

it seems you need to use the floating arithmetic in each sub-expression. That is write

QuizGrade = (TotalQuiz/50.0f)*100*0.50f;
MidtermExamGrade = (midterms/100.0f)*100*0.20f;
FinalExamGrade = (finals/100,0f)*100*0.30f;

Also in this statement

TotalQuiz = q1 + q2 +  q3 + q4 + q5;

you are using uninitialized variables q1, q2, q3, q4, q5. This statement does not make a sense. Maybe you mean using data members of the structure q1, q2, q3, q4, q5.

I do not know but maybe you want in the for loop ro write

for (i = 0; i < n; ++i) 
{
      printf("Quiz 1: ");
      scanf("%d", &s[i].q1

      q1 = s[i].q1;

     // and so on
);