-2

I don't know why it kept getting an error at the line, where the program will display the grade:

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h> 
const char ConvertToGrade(int mark);

int main() {

    int mark;
    printf("Enter your mark : ");
    scanf("%d", &mark);
    printf("\nYour grade is : %s", ConvertToGrade(mark));
}

const char ConvertToGrade(int mark) {
    char grade;
    if (mark >= 0 && mark < 50)
        grade= 'F';
    else if (mark >= 50 && mark < 60)
        grade = 'D';
    else if (mark >= 60 && mark < 70)
        grade = 'C';
    else if (mark >= 70 && mark < 80)
        grade = 'B';
    else if (mark >= 80 && mark < 101)
        grade = 'A';
    else
        grade = 'X';
    return grade;
}

This is the question asking for the C Program

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
LuqTozaki
  • 7
  • 1

2 Answers2

0

This line is causing the error

printf("\nYour grade is : %s", ConvertToGrade(mark));

You are using %s whereas you are returning a const char. Here's the corrected code-

printf("\nYour grade is : %c", ConvertToGrade(mark));
Dhruva-404
  • 168
  • 2
  • 10
  • Newlines go at the end of a message, not at the beginning! (Or, newlines go at the end of a message, and only optionally do you put a newline at the beginning — the newline at the end is important.) – Jonathan Leffler Feb 09 '21 at 06:21
  • Is this better? – Dhruva-404 Feb 09 '21 at 06:24
  • No. You need: `printf("\nYour grade is %c\n", ConvertToGrade(mark));` or `printf("Your grade is %c\n", ConvertToGrade(mark));` — and I'd expect to use the second of those options. I removed the colon too, but removing the space before it would work too (and you could leave the colon and spaces as is if you really insist, though I'd argue that it isn't necessary). – Jonathan Leffler Feb 09 '21 at 06:27
  • Well i have only corrected the error and kept same the formatting of the output. – Dhruva-404 Feb 09 '21 at 07:05
0

printf("\nYour grade is : %s", ConvertToGrade(mark));

You are using %s as format specifier for char. You should use %c for char.