我给 %f 的输入没有保存
The input that i give to my %f does not save
gradeOne和secondGrade不保存我给的数字。所以我不能得到平均值,因为,总是以 (0 + 0)/2 的除法结尾,有人可以帮助我吗?
#include <stdio.h>
#include <stdlib.h>
int main()
{
int students;
char name[50];
double gradeOne, secondGrade, classAverage = 0, average = 0;
for(students = 1;students <= 15; students++){
printf("Tell me Your name: \n");
fflush(stdin);
scanf("%[^\n]s", &name);
//saves name
printf("Tell me your first grade: \n");
scanf("%f", &gradeOne);
printf("Tell me your second grade: \n");
scanf("%f", &secondGrade);
//saves grade
printf("\n========================================\n");
printf("Student: %s\n", name);
printf("First Grade: %3.2f \nSecond Grade: %3.2f \n", gradeOne, secondGrade);
printf("%f", average);
printf("\n========================================\n");
average = (gradeOne + secondGrade)/2;
//creates average
printf("Average of the %d student: %3.2f",students, average);
classAverage += average;
//creates class average
}
classAverage = classAverage / (students - 1);
printf("The class average was: %3.2f", classAverage);
return 0;
}
scanf()
中的%f
用于阅读float
。您应该使用 %lf
来阅读 double
。
请注意,您应该在 printf()
中使用 %f
进行 打印 double
。较新的规范允许 %lf
用于 printf()
,但 %f
应该更好地兼容。
gradeOne和secondGrade不保存我给的数字。所以我不能得到平均值,因为,总是以 (0 + 0)/2 的除法结尾,有人可以帮助我吗?
#include <stdio.h>
#include <stdlib.h>
int main()
{
int students;
char name[50];
double gradeOne, secondGrade, classAverage = 0, average = 0;
for(students = 1;students <= 15; students++){
printf("Tell me Your name: \n");
fflush(stdin);
scanf("%[^\n]s", &name);
//saves name
printf("Tell me your first grade: \n");
scanf("%f", &gradeOne);
printf("Tell me your second grade: \n");
scanf("%f", &secondGrade);
//saves grade
printf("\n========================================\n");
printf("Student: %s\n", name);
printf("First Grade: %3.2f \nSecond Grade: %3.2f \n", gradeOne, secondGrade);
printf("%f", average);
printf("\n========================================\n");
average = (gradeOne + secondGrade)/2;
//creates average
printf("Average of the %d student: %3.2f",students, average);
classAverage += average;
//creates class average
}
classAverage = classAverage / (students - 1);
printf("The class average was: %3.2f", classAverage);
return 0;
}
scanf()
中的%f
用于阅读float
。您应该使用 %lf
来阅读 double
。
请注意,您应该在 printf()
中使用 %f
进行 打印 double
。较新的规范允许 %lf
用于 printf()
,但 %f
应该更好地兼容。