Reputation: 11
Here is what I have tried:
#include<stdio.h>
int main()
{
int count=0;
int x;
char Grade;
char studentName[50];
int studentID=0;
int f=0;
do
{
printf("\nEnter student ID: ");
scanf("%d", &studentID);
if(studentID!=0)
{
printf("Enter student name: ");
scanf("%s", &studentName);
printf("Enter student marks: ");
scanf("%d", &x);
if(x<=100 && x>=80)
Grade = 'A';
else if(x<=79 && x>=75)
Grade = 'A-';
else if(x<=74 && x>=70)
Grade = 'B+';
else if(x<=69 && x>=65)
Grade = 'B';
else if(x<=64 && x>=60)
Grade = 'B-';
else if(x<=59 && x>=55)
Grade = 'C+';
else if(x<=54 && x>=50)
Grade = 'C';
else if(x<=49 && x>=45)
Grade = 'C-';
else if(x<=44 && x>=40)
Grade = 'D+';
else if(x<=39 && x>=35)
Grade = 'D';
else if(x<=34 && x>=30)
Grade = 'D-';
else if(x<=29 && x>=0)
{
Grade = 'F';
f++;
}
printf("%s have the following marks %d and the Grade is %c\n ", studentName, x, Grade);
count++;
}
}while(studentID!=0);
printf("Sum of student = %d\n", count);
printf("Sum of fail student = %d\n", f);
}
I get the following error when trying to compile:
"[Warning] multi-character character constant [-Wmultichar]".
I can execute the code but the error keeps bothering me. Also I cannot get the result which have - Grade.
Please advice as I am new learner. Thanks.
Upvotes: 0
Views: 699
Reputation: 3496
As pointed out the error is that you are trying to force 2 char
into a variable that can only contain 1, and this is why you cannot have grades with "-", and I guess you don't have those with the "+" neither : it seems that, in your case (cf @mafso comment), the program only takes the 1st char
(the letter), the rest is lost.
Let the compiler adjust the size of your containers for you:
// With "[]", the compiler will reserve just what we need
const char * grades[] =
{
"F", "D-", "D", "D+", "C-", /* etc... */ "A-", "A+"
};
Also, I think we can do a little mathematical trick to get directly the string-grade from the rating and avoid "waterfall" else if
:
size_t index;
if(x < 25) // Safety measure
{
index = 0; // F
}
else if (x > 79) // [edit] ok we need another one
{
index = 11; // A (or should it be A+ ?)
}
else // The real trick
{
index = (x/5 + 1) - 6 ;
}
// You need 3 chars: one for the letter, one for the + (or -) and the
// last one for the NULL-terminating '\0' which is the "end-of-string" byte
char student_grade[3];
strcpy(student_grade, grades[index]);
And now you understand why I started grades
from "F-"
to "A"
:)
Upvotes: 2
Reputation: 2382
The error is quite indicative of what is wrong with your code. A multi-character character constant indicates you are trying to assign more than one character to a character constant which is wrong. As pointed by mahendiran.b you cannot assign "A-" or "B-" to Grade as it takes only a single character. It should be a string.
Upvotes: 1