英文:
Switch statements- grading system
问题
#include <stdio.h>;
void main() {
int a, b, c, d, e, f, avg;
printf("Enter marks of six subjects: ");
scanf("%d %d %d %d %d %d", &a, &b, &c, &d, &e, &f);
avg = (a + b + c + d + e + f) / 6;
printf("Average is %d", avg);
switch (avg) {
case 10:
printf("Your grade is 10.");
break;
case 9:
printf("Your grade is 9.");
break;
case 8:
printf("Your grade is 8.");
break;
case 7:
printf("Your grade is 7.");
break;
case 6:
printf("Your grade is 6.");
break;
case 5:
printf("Your grade is 5.");
break; // You missed the 'break' statement here.
default:
printf("You are fail.");
}
}
英文:
#include <stdio.h>
void main() {
int a, b, c, d, e, f, avg;
printf("Enter marks of six subjects: ");
scanf("%d %d %d %d %d %d %d", &a, &b, &c, &d, &e, &f);
avg = (a + b + c + d + e + f) / 6;
printf("Average is %d", avg);
switch (avg) {
case 10:
printf("Your grade is 10.");
break;
case 9:
printf("Your grade is 9.");
break;
case 8:
printf("Your grade is 8.");
break;
case 7:
printf("Your grade is 7.");
break;
case 6:
printf("Your grade is 6.");
break;
case 5:
printf("Your grade is 5.");
default:
printf("You are fail.");
}
}
After I enter the marks of six subjects, the code is not running, it's neither showing any error message as well.
答案1
得分: 1
以下是您要翻译的内容:
Problems include
Not enabling all compiler warnings
未启用所有编译器警告
Mis-match
不匹配
"%d %d %d %d %d %d %d", &a, &b, &c, &d, &e, &f
: 7 specifiers and 6 following arguments.
"%d %d %d %d %d %d %d", &a, &b, &c, &d, &e, &f
:7个格式说明符和6个后续参数。
Not checking return value of scanf()
未检查scanf()
的返回值
// scanf("%d %d %d %d %d %d %d", &a, &b, &c, &d, &e, &f);
if (scanf("%d %d %d %d %d %d", &a, &b, &c, &d, &e, &f) != 6) {
; // TBD: Handle error, maybe print some error message and exit
}
No break
没有break语句
case 5:
printf("Your grade is 5.");
break; // add
default:
printf("You are fail.");
英文:
Problems include
Not enabling all compiler warnings
Mis-match
"%d %d %d %d %d %d %d", &a, &b, &c, &d, &e, &f
: 7 specifiers and 6 following arguments.
Not checking return value of scanf()
// scanf("%d %d %d %d %d %d %d", &a, &b, &c, &d, &e, &f);
if (scanf("%d %d %d %d %d %d", &a, &b, &c, &d, &e, &f) != 6) {
; // TBD: Handle error, maybe print some error message and exit
}
No break
case 5:
printf("Your grade is 5.");
break; // add
default:
printf("You are fail.");
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论