How to fix the code below so it will run the error message: [Error] invalid oper
ID: 3801169 • Letter: H
Question
How to fix the code below so it will run
the error message: [Error] invalid operands of types 'const char [3]' and 'int' to binary 'operator&'
in the C language Write a program that assigns test score grades. The program's input should be a series of test scores of type int. Have the program read each score and print either the corresponding grade or an appropriate error message. The program is to continue until end-of-file. If invalid character data is entered, you will need to flush the invalid characters so that test scores can continue to be entered until end-of-file.
#include <stdio.h>
int main()
{
int score;
printf("Enter your score between 0 to 10 ");
scanf("%d", &score);
while((r=scanf("%i" & score))!=EOF)
{
if(r==0)
{
printf("invalid char");
while(getchar()!=' ');
}
else
switch(score)
{
case 10 :
case 9 :
/* score between 9-10 */
printf("Your Grade : A " );
break;
case 8 :
/* score 8 */
printf("Your Grade : B " );
break;
case 7 :
/* score 7 */
printf("Your Grade : C " );
break;
case 6 :
/* score 6 */
printf("Your Grade : D " );
break;
case 5 :
case 4 :
case 3 :
case 2 :
case 1 :
case 0 :
/* score between 0-5*/
printf("Your Grade : F " );
break;
default :
/* score less than 0 */
if(score<0)
printf("Negative Number " );
if(score>10)
printf("Value greater than 10 " );
if(score >= 'a' && score <= 'z' || score >= 'A' && score <= 'Z')
printf("A character value " );
}
}
return 0;
}
Explanation / Answer
Problem Solved
#include <stdio.h>
int main()
{
int score,r;
printf("Enter your score between 0 to 10 ");
while((r=scanf("%i" ,& score))!=EOF)
{
if(r==0)
{
printf("invalid char");
while(getchar()!=' ');
}
else
switch(score)
{
case 10 :
case 9 :
/* score between 9-10 */
printf("Your Grade : A " );
break;
case 8 :
/* score 8 */
printf("Your Grade : B " );
break;
case 7 :
/* score 7 */
printf("Your Grade : C " );
break;
case 6 :
/* score 6 */
printf("Your Grade : D " );
break;
case 5 :
case 4 :
case 3 :
case 2 :
case 1 :
case 0 :
/* score between 0-5*/
printf("Your Grade : F " );
break;
default :
/* score less than 0 */
if(score<0)
printf("Negative Number " );
if(score>10)
printf("Value greater than 10 " );
if(score >= 'a' && score <= 'z' || score >= 'A' && score <= 'Z')
printf("A character value " );
}
}
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.