Muhtasim Rabib
Muhtasim Rabib

Reputation: 43

C program to calculate a student's grade

This is a very simple program, but i don't know why i can't run it properly. After compilation I can manage to ask an input of the student's grade but i cannot run the if statements. Here is my code:

#include <stdio.h>
#include <stdlib.h>

int main()
{
    // Write a program that would determine a student's grade based on the set ranges

    float grade;

    printf("Please enter the grade for the student (in percentages) : \n");
    scanf("%f", grade);

    if (grade>=90){
        printf("The student got an A");
    }
    else if (grade>=70 && grade <= 89.99 ){
        printf("The student got a B");
    }
    else if (grade>=50 && grade >= 69.99){
        printf("The student got a C");
    }
    else {
        printf("The student failed");
    }
return 0;
}

Upvotes: 0

Views: 3761

Answers (1)

Alberto
Alberto

Reputation: 12949

You have to pass the pointer to the element where you want to save the input:

scanf("%f", &grade);

Infact you should have a warning from your compiler complaining about this (I'm using clang, other compilers may needs some extra flags):

warning: format specifies type 'float *' but the argument has type 'double' [-Wformat]
scanf("%f", grade);
       ~~   ^~~~~

Upvotes: 1

Related Questions