struct student{
char name[20]; /* student name */
double testScore; /* test score */
double examScore; /* exam score */
double total; /* total score = test+exam scores */
};
void computeScore(student information);
int main()
{
struct student information[50];
for (i = 0; i < 50; i++)
{
printf("Enter the student name: ");
gets(information[i].name);
printf("\nEnter test score: ");
scanf("%lf",information[i].testScore);
printf("\nEnter exam score: ");
scanf("%lf",information[i].examScore);
computeScore(information[i]);
}
}
我尝试输入输入测试分数,但在测试分数之后我的程序立即终止。有任何想法吗?连Enter考试分数都没有达到。
最佳答案
发生这种情况是因为变量information,它是结构“student”的一个对象,您在其中将变量定义为“double”。这意味着当程序尝试使用 to 结构读取 double 数据类型的值时发现错误。
Borland 的编译器不会链接浮点 (f-p) 库,除非我们需要它。因此,当 scanf() 或 printf() 调用中有“%f”或其他浮点 (f-p) 格式时,我们需要强制添加任何浮点 (f-p) 函数。
void dummy(double*a) {
double b=*a; //perform some floating access
dummy (&b); //calling a floating point function
}
只需在您的程序中添加此函数即可。
关于c - 无法在结构中输入值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22446875/