Switch statement
My program couldn't run at first
#include <>
void main()
{
int score, grade;
scanf("%d", &score);
if (score < 0 || score>100)
printf("Input error!");
else grade = score / 10;
switch (grade)
{
case0:printf(“G is E/n”); break;
case1:
case2:
case3:
case4:
case6: printf(“G is D/n”); break;
case7: printf(“G is C/n”); break;
case8: printf(“G is B/n”); break;
case9: printf(“G is A/n”); break;
case10:
}
}
The error that occurs is a syntax error "}"
The problem has not been resolved yet
What the teacher said
#include<>
int main()
{
int a;
scanf("%d", &a);
if (a < 0 || a>100)
{
printf(“error”);
}
else
{
switch (a / 10)
{
case 0:
case 1:
case 2:
case 3:
case 4:
case 5:printf("Student level is 'E'\n");break;
case 6:printf("Student level is 'D'\n");break;
case 7:printf("Student level is 'C'\n");break;
case 8:printf("Student level is 'B'\n");break;
case 9:printf("Student level is 'A'\n");break;
case 10:printf("Student level is 'A'\n");break;
}
}
}
The correct program after modification:
#include <>
void main()
{
int score, grade;
scanf("%d", &score);
if (score < 0 || score>100)
printf("Input error!");
else
{
grade = score / 10;
}
switch (grade)
{
case 0:
case 1:
case 2:
case 3:
case 4:
case 5: printf(“grade is E\n”); break;
case 6: printf(“grade is D\n”); break;
case 7: printf(“grade is C\n”); break;
case 8: printf(“grade is B\n”); break;
case 9: printf(“grade is A\n”); break;
case 10: printf(“grade is A\n”); break;
}
}
The error is to type \ as /, no spaces are added to case 0
My program couldn't run at first
#include <>
void main()
{
int score, grade;
scanf("%d", &score);
if (score < 0 || score>100)
printf("Input error!");
else grade = score / 10;
switch (grade)
{
case0:printf(“G is E/n”); break;
case1:
case2:
case3:
case4:
case6: printf(“G is D/n”); break;
case7: printf(“G is C/n”); break;
case8: printf(“G is B/n”); break;
case9: printf(“G is A/n”); break;
case10:
}
}
The error that occurs is a syntax error "}"
The problem has not been resolved yet
What the teacher said
#include<>
int main()
{
int a;
scanf("%d", &a);
if (a < 0 || a>100)
{
printf(“error”);
}
else
{
switch (a / 10)
{
case 0:
case 1:
case 2:
case 3:
case 4:
case 5:printf("Student level is 'E'\n");break;
case 6:printf("Student level is 'D'\n");break;
case 7:printf("Student level is 'C'\n");break;
case 8:printf("Student level is 'B'\n");break;
case 9:printf("Student level is 'A'\n");break;
case 10:printf("Student level is 'A'\n");break;
}
}
}
The correct program after modification:
#include <>
void main()
{
int score, grade;
scanf("%d", &score);
if (score < 0 || score>100)
printf("Input error!");
else
{
grade = score / 10;
}
switch (grade)
{
case 0:
case 1:
case 2:
case 3:
case 4:
case 5: printf(“grade is E\n”); break;
case 6: printf(“grade is D\n”); break;
case 7: printf(“grade is C\n”); break;
case 8: printf(“grade is B\n”); break;
case 9: printf(“grade is A\n”); break;
case 10: printf(“grade is A\n”); break;
}
}
The error is to type \ as /, no spaces are added to case 0