[Solved] cannot convert char to char & illegal use of floating point


Please see solution. Your original solution had a lot of issues and is fundamentally flawed. The above commentary highlight most of the mistakes you’ve made, but the main flaw with your original solution was the lack of clear process structure.

Recommend drafting out your algorithm or process flow before you begin coding in future

1. Grab user input for student name and number of subjects
2. For every subject
    a. Get user to input grade
    b. Check grade is valid
    c. Add to cumulative GPA value
   Until num_subjects is met
3. Print out Student Name, Num Subjects and his GPA (cgpa/num_subjects)

See sample solution below, which adheres to the process I defined above.
I hope this assists you in your programming journey 🙂

#include <stdio.h>
// #include <conio.h> - Commented this out because this is MS Dos specific and makes solution non portable
#include <ctype.h>

int main(void)
{
    int num_subjects;
    char name[10];
    char grade;
    float cgpa=0.0;
    int x;

    // These do not need to be within your loop. Especially num_subjects
    printf("\nEnter Student Name: \n");
    scanf("%s", &name[0]);

    printf("\nEnter the number of subjects? \n ");
    scanf("%d", &num_subjects);

    // I've replaced this with a while loop, because you need a continuous loop until a valid grade is required
    while( x < num_subjects )
    {
        printf("\nEnter Student Grade: \n");
        scanf("%c", &grade);

        // Upper case the value, so there is no ambiguity in 'a' or 'A'
        grade = toupper(grade);

        printf("\nGrade Entered: %c\n", grade);
        if (grade == 'A') {
            cgpa+=4.0;
        }
        else if (grade == 'B') {
            cgpa+=3.0;
        }
        else if (grade == 'C') {
            cgpa+=2.0;
        }
        else if (grade == 'D') {
            cgpa+=1.3;
        }
        else if (grade == 'F') {
            cgpa+=0.1;
        }
        else {
            printf("You've entered a wrong grade");

            // Being lazy here. I'm decrementing the counter, because I am lazy.
            // By right, the efficient thing to do is to increment the counter on a valid value
            // But in the interest of writing less code, I've decided to decrement the value on an invalid value.
            // And add more comments :P
            x--;
        }

        // Increment x if a valid grade was entered.
        x++;
    }

    // Final output line
    printf("\nStudent: %s, Number Subjects: %d, GPA: %.2f", name, num_subjects, cgpa/num_subjects);
}

1

solved cannot convert char to char & illegal use of floating point