scanf()只读第一个输入(数字)
我无法真正解释它,只不过scanf()
只读取第一个值,然后基于此计算出计算结果。scanf()只读第一个输入(数字)
int main() {
int i, students = 0;
char name[20];
int tests;
float test_score;
int test_sum = 0;
char letter_grade;
double test_average;
printf("Number of students: ");
scanf("%d", &students);
for (i = 0; i < students; i++) {
printf("\nStudent name %d: ", i + 1);
scanf(" %s", &name);
fflush(stdin);
printf("Number of test(s) for %s: ", name);
scanf("%d", &tests);
fflush(stdin);
printf("Enter %d test score(s) for %s: ", tests, name);
if (i < students) {
scanf("%f", &test_score);
test_sum += test_score;
test_average = test_sum/(float)tests;
}
printf("Average test score: %.2f", test_average);
fflush(stdin);
}
return 0;
}
说我进入2名学生,有2测试成绩第一的学生,然后进入45 87.我应该得到66.00,但我得到22.50。对于第二个学生,我会输入100 55 87的3个测试分数,而我得到48.33。 Waaayyy关闭。
我知道我做错了什么,但我无法弄清楚,因为我之前工作过,但循环不会继续给第二个学生。
if (i < students) {
scanf("%f", &test_score);
test_sum += test_score;
test_average = test_sum/(float)tests;
}
应该是:
test_sum = 0;
for (int j = 0; j < tests; j++) {
scanf("%f", &test_score);
test_sum += test_score;
}
test_average = test_sum/(float)tests;
这似乎是获得正确计算的唯一方法,但是我的程序刚刚结束,并且没有到达#2学生。我也删除了fflush(stdin)。我已经圈了几个小时...... – DSmith
test_sum变量在for循环之前应该设置为0 –
您总是需要检查返回值scanf()
以查看它读取的令牌数量。如果阅读失败,则需要采取纠正措施。
不清楚为什么每次都需要fflush(stdin)
。
张贴的代码包含了几个问题,包括
- 只能输入一个测试得分
- 的
int
随机搭配和float
和double
变量 - 注册CT格式字符串调用
scanf()
- 许多未用的变量
- 故障检查错误上调用
scanf()
- 差变量命名。变量名应说明的内容或使用(或更好两者)
- 调用
fflush(stdin)
被明确列为C标准 - test_sum未定义行为是不是学生
之间重新初始化以下建议代码修复所有上面的问题和编译干净
#include <stdio.h>
#include <stdlib.h> // exit(), EXIT_FAILURE
// prototypes
void flushStdin(void);
int main(void)
{
int numStudents = 0;
char studentName[20];
int numTests;
double test_score;
double test_sum = 0.0;
//char letter_grade;
double test_average;
printf("Number of students: ");
if(1 != scanf("%d", &numStudents))
{ // then scanf failed
perror("scanf for number of students failed");
exit(EXIT_FAILURE);
}
// implied else, scanf successful
flushStdin();
for (int i = 0; i < numStudents; i++)
{
printf("\nStudent name %d: ", i + 1);
if(1 != scanf(" %s", studentName))
{ // then scanf failed
perror("scanf for student name failed");
exit(EXIT_FAILURE);
}
// implied else, scanf successful
flushStdin();
printf("Number of test(s) for %s: ", studentName);
if(1 != scanf("%d", &numTests))
{ // scanf failed
perror("scanf for number of tests failed");
exit(EXIT_FAILURE);
}
// implied else, scanf successful
test_sum = 0.0;
printf("Enter %d test score(s) for %s: ", numTests, studentName);
for(int j=0; j<numTests; j++)
{
if(1 != scanf("%lf", &test_score))
{ // then scanf failed
perror("scanf for test score failed");
exit(EXIT_FAILURE);
}
// implied else, scanf successful
flushStdin();
test_sum += test_score;
}
test_average = test_sum/numTests;
printf("Average test score: %.2lf", test_average);
}
return 0;
} // end function: main
void flushStdin()
{
int ch;
while((ch = getchar()) != EOF && '\n' != ch);
}
你知道什么'fflush(stdin)'做? – EOF
'if(i for(int j = 0; j
BLUEPIXY
@EOF清除缓冲区。当我不包含它时,我的程序结束。我确实看到它是多么的多余,但我不知道为什么我的代码不会没有它们。 – DSmith